[patch] Use select_related() when retrieving a user profile
|Reported by:||Matt Riggott||Owned by:||nobody|
|Cc:||Triage Stage:||Design decision needed|
|Has patch:||yes||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||no|
To start with an example:
>>> from django.contrib.auth.models import User >>> u = User.objects.get(pk=1) >>> p = u.get_profile() >>> p.user.username 'dave' >>> from django.db import connection >>> len(connection.queries) 3
Even though I'm dealing with two database objects (a User and its profile) three queries were executed. The first to get the user, the second to get the profile, and the third to get the user again.
If line 257 in django/contrib/auth/models.py was changed to use
select_related the database would only be hit twice.
It's a minor thing, but I thought I'd mention it. You know, just in case you guys ran out of patches to test and such.
(If a user profile has lots of foreign key fields this could result in a lot of objects being created, which is why I've limited the depth to 1.)
Change History (6)
comment:1 Changed 9 years ago by
|Patch needs improvement:||unset|
|Triage Stage:||Unreviewed → Design decision needed|