Re: Foreign key triggered some redundant SQL queries

2009-07-27 Thread Karen Tracey
On Mon, Jul 27, 2009 at 4:04 AM, Tom 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

Re: Foreign key triggered some redundant SQL queries

2009-07-27 Thread Tom
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

Re: Foreign key triggered some redundant SQL queries

2009-07-27 Thread Tom
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 wrote: > On Sun, Jul 19, 2009 at 10:48 AM, aXqd wrote: > > > Hi, all: > > > Recently, I linked two

Re: Foreign key triggered some redundant SQL queries

2009-07-19 Thread aXqd
在 2009-7-20,0:33,Alex Gaynor 写到: > > On Sun, Jul 19, 2009 at 10:48 AM, aXqd 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

Re: Foreign key triggered some redundant SQL queries

2009-07-19 Thread Alex Gaynor
On Sun, Jul 19, 2009 at 10:48 AM, aXqd 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

Foreign key triggered some redundant SQL queries

2009-07-19 Thread aXqd
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*