Also, I strongly recommend reading through this document for an idea of
when queries do and don't hit the database:
https://docs.djangoproject.com/en/1.5/topics/db/optimization/
Additionally, take a look at django-debug-toolbar, which will show you
all the queries that go into returning a view, ho
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
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().
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
Hi all,
Is there any shortcut (minimum db hit) for the following querysets:
p = Person.objects.get(mail="pers...@ex.com")
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
5 matches
Mail list logo