It is quite common to have templates with lists traversing reverse
foreignkey relationships or m2m relationships, which generate a lot of
queries, for example:

        {% for building in buildinglist %}
          <p>Building: {{ building }}</p>
          <p>Appartments:</p>
          <ul>
            {% for appartment in building.appartment_set.all %}
              <li>{{ appartment.number }}</li>
            {% endfor %}
          </ul>
        {% endfor %}

to reduce the querycount you can use some tricks to fetch the child
items for each parent in a separate query.

I abstracted these tricks into a custom manager and created a project
for it, called django-selectreverse.

It allows you to reduce the querycount in the example above to only
two queries, regardless of the number of buildings.

All you need to do is use this custom manager and in your view use it
to prefetch the child elements like this:
buildinglist = Building.objects.select_reverse({'appartments':
'appartment_set'})

Want to know more? Check out django-selectreverse at
http://code.google.com/p/django-selectreverse/ .
--~--~---------~--~----~------------~-------~--~----~
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+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to