Found the answer to my question just after posting... for item in FOO.objects.select_related('bar'): print item.bar.id
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 -~----------~----~----~----~------~----~------~--~---