Reduce queries for user profiles.
Review Request #4824 — Created Oct. 21, 2013 and submitted — Latest diff uploaded
Reduce queries for user profiles.
Django's impending removal of get_profile() meant that we had to move to
our own queries for Profile. We ended up using Profile.objects.get and
get_or_create queries elsewhere, but in code paths where a profile was
accessed twice, this resulted in extraneous queries.User.get_profile is now brought back, and it's under our control. We now
provide our own implementation of this that will survive their removal.
It's more performant in that it doesn't have to attempt an import and
some other logic to get the Profile model, since it can now use the
model directly. get_profile will cache the profile for future lookups.Profile.objects.get_or_create is aware of the Profile cache, and will
return directly if it's set. It falls back on the standard
get_or_create, and will set the cache on the User before returning. This
means that, unlike how things used to be, subsequent get_or_create calls
will still do at most only one query.
Unit tests pass.
Saw fewer queries when doing multiple calls.