> i have to check the method's return value first with an 'if'
> tag than run the query again for the 'for' loop. this doubles
> the load on the db what i don't like, not to mention that the
> two queries may return different lists.

Django's ORM should cache your query results, though it may need 
to be primed with your results first (if the query object doesn't 
already have results, it issues a COUNT(*) call first, and then 
your request for results makes for a second call; but if they 
were done in the other order, the len/bool testing would hit the 
internal warmed cache first).  Alternatively, you could force the 
results to be a list before passing it to your render:

   myitems = list(MyModel.objects.filter(...))
   return render_to_response("template.html",
     {"myitems": myitems})

This will allow O(1) calculation of len/bool properties, and only 
hit the DB once.

> am i missing something? do i follow a bad pattern? why i can't
> extend the context from inside a template?

You mostly don't want to try to do this in the template, but want 
to solve the problem in the view.  It helps keep the template 
clean and puts such logic where it belongs.

-tim




--~--~---------~--~----~------------~-------~--~----~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to