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.

Thanks for any help...

Regards,
Tom

On Jul 19, 11:33 pm, Alex Gaynor <alex.gay...@gmail.com> wrote:
> On Sun, Jul 19, 2009 at 10:48 AM, aXqd<axqd...@gmail.com> wrote:
>
> > Hi, all:
>
> > Recently, I linked two models ( FOO & BAR ) with a foreign key.
> > Then I retrieved the whole records back with the following codes:
>
> > 1 for item in FOO.objects.all():
> > 2   print item.bar.id
>
> > In my opinion, the whole thing should be lazy enough.
> > So if Line 1 got all the BAR id back, *WHY* did Line 2 still trigger a
> > SQL query to retrieve the whole BAR object while all I want here was
> > just that ID, which, I think, was already known by django at this
> > time?
> > Am I missing something here?
>
> > Thanks.
>
> > --
> > Regards.
> > -Tian
>
> Try doing item.bar_id.  Doing item.bar causes Django to load up the
> bar object, but if all you want is the id you can use "bar_id".
>
> Alex
>
> --
> "I disapprove of what you say, but I will defend to the death your
> right to say it." -- Voltaire
> "The people's good is the highest law." -- Cicero
> "Code can always be simpler than you think, but never as simple as you
> want" -- Me

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