On Mon, Jul 27, 2009 at 4:04 AM, Tom <tba...@gmail.com> wrote:

>
> Hi,
>
> Thanks for the tip...
>
> But I've got a similar problem that needs the data from the linked
> model:
>
> (simple example)
>
> for school in SCHOOL.objects.all():
>   print school.student.name
>
> When I run this in Django it makes a separate SQL call every time it
> tries to get school.student.name.
>
> It should only take a single SQL call to get all the relevant info for
> this loop... you can join the school and student tables and explicitly
> ask for "student.name" from the joined school/student tables.
>
> Is there no way to tell Django to create a query with an explicit join
> and explicitly add "student.name" to the results? Or do I need to
> resort to writing my own SQL? This seems like a pretty common
> situation so I figured Django would have some magic up its sleeves,
> but I can't find anything in the docs.
>

You seem to have missed select_related:

http://docs.djangoproject.com/en/dev/ref/models/querysets/#id4

Karen

--~--~---------~--~----~------------~-------~--~----~
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