[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)
Changed 8 years ago by Matt Riggott
comment:1 Changed 8 years ago by Gary Wilson <gary.wilson@…>
- Needs documentation unset
- Needs tests unset
- Patch needs improvement unset
- Triage Stage changed from Unreviewed to Design decision needed
comment:4 Changed 6 years ago by anonymous
- Resolution wontfix deleted
- Status changed from closed to reopened