Tuesday, April 26, 2011

Re: How to write a view that accesses a method over all objects in a given model?

Hi,

to access a model in the view, simply import it into you view.
Something along the lines:

from yourapp.models import Place
from django.http import HttpResponse

def some_view(request):
OBJECT_ID = 1 # put here the id of the object
# or give it as a function argument
place = Place.object.get(id=OBJECT_ID)
coordinates= place.coordinates()
return HttpResponse("The coordinates of the place number %s are:
%s"\
% (OBJECT_ID, coordinates) # assuming
Location has

# a string representation

You can also use django.db.models.get_model, which returns a model
class from a app_label, model_name tuple, although i would only use it
when the model names is not known at the compiling time.

Hope it helps.

Alendit.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate