1. No, the two will execute identically. 2. No, `group` is loaded by select_related, and `name` is a field on group, so it's loaded when group is, unless you explicitly exclude it. 3. That's one query. However, if you're only grabbing the first result, select_related isn't even necessary... just do: name = p.groupregistration_set.all()[:1][0].group.name However, note that BOTH lines will fail if the query returns no results, due to the [0] index. I.e., if p.groupregistration_set.all() returns 0 results, then the [0] is invalid. 4. I'm not sure I understand the question, could you expand?
_Nik On 3/7/2013 10:35 AM, Fatih Tiryakioglu wrote: > Thank you Nicolas, > > This works: > reg = p.groupregistration_set.all().select_related('group') > name = reg[0].group.name > > This also works: > name = p.groupregistration_set.all().select_related('group')[0].group.name > > > 1) Is there any difference? Which one is beter. > 2) reg = p.groupregistration_set.all().select_related('group') > name = reg[0].group.name > does second line result in db hit??? > 3) name = > p.groupregistration_set.all().select_related('group')[0].group.name > this is 1 or 2 hit?? > > and an optional 10 points question: > 4) If I had two members of reg list, how to modify the code above? > > > Thanks again.. > > > -- > > > > > -- > > On Thursday, March 7, 2013 6:35:14 PM UTC+2, Nikolas Stevenson-Molnar > wrote: > > The biggest hit is probably going to be the reg.group call. You can > optimize that by using select_related (), which will take what is > currently 1 + (number of group registrations for the user) queries > and > turn it into 2 queries: > > reg = p.groupregistration_set.all().select_related('group') > > More on select_related: > https://docs.djangoproject.com/en/dev/ref/models/querysets/#select-related > > <https://docs.djangoproject.com/en/dev/ref/models/querysets/#select-related> > > > _Nik > > On 3/6/2013 11:18 PM, Fatih Tiryakioglu wrote: > > Hi all, > > > > Is there any shortcut (minimum db hit) for the following querysets: > > > > p = Person.objects.get(mail="pers...@ex.com <javascript:>") > > p.password & form.password comparison and it is ok. > > > > #reversing to the person's groupregisteration, for example: > > reg = p.groupregistration_set.all() #groupregistratin model is > simply > > person (foreign key) and group (foreign key) > > > > #evaluating group of reg list. > > g = reg.group (for the every member of reg list) > > > > #and finally group name > > n = g.name <http://g.name> (for the every member of g list) > > > > Which queryset or querysets hits the db, and how can I minimize it. > > > > > > > > Thanks. > > -- > > > > > > -- > > You received this message because you are subscribed to the Google > > Groups "Django users" group. > > To unsubscribe from this group and stop receiving emails from > it, send > > an email to django-users...@googlegroups.com <javascript:>. > > To post to this group, send email to django...@googlegroups.com > <javascript:>. > > Visit this group at > http://groups.google.com/group/django-users?hl=en > <http://groups.google.com/group/django-users?hl=en>. > > For more options, visit https://groups.google.com/groups/opt_out > <https://groups.google.com/groups/opt_out>. > > > > > > -- > You received this message because you are subscribed to the Google > Groups "Django users" group. > To unsubscribe from this group and stop receiving emails from it, send > an email to django-users+unsubscr...@googlegroups.com. > To post to this group, send email to django-users@googlegroups.com. > Visit this group at http://groups.google.com/group/django-users?hl=en. > For more options, visit https://groups.google.com/groups/opt_out. > > -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at http://groups.google.com/group/django-users?hl=en. For more options, visit https://groups.google.com/groups/opt_out.