views:

25

answers:

1

The Django documentation gives en example like so:

b = Blog.objects.get(id=1)
b.entry_set.all()

Which from what I understand results in 2 queries. What if I wanted to get the blog, the blog entries and all the comments associated with that entry in a number of queries that does not depend on the number of entries? Or do I have to drop down to SQL to do that?

+2  A: 
Blog.objects.select_related(...).get(id=1)
Ignacio Vazquez-Abrams
I do not believe that works (http://groups.google.com/group/django-developers/browse_thread/thread/1e1fcee95759c370). select_related() does work for "forward" relationships, but not for "backward" ones. In this case Entries to Blogs is a "backward" relationship. Surely there is a way to do this efficiently.
ipartola