#8660: Calling User.get_profile() should pre-populate the user foreign key on 
the
profile object
-------------------------------------+--------------------------------------
          Reporter:  mmalone         |         Owner:  nobody                 
            Status:  new             |     Milestone:  post-1.0               
         Component:  Authentication  |       Version:  SVN                    
        Resolution:                  |      Keywords:  cache profile user auth
             Stage:  Accepted        |     Has_patch:  0                      
        Needs_docs:  0               |   Needs_tests:  0                      
Needs_better_patch:  0               |  
-------------------------------------+--------------------------------------
Old description:

> When you use the User.get_profile() method to fetch a profile object, it
> doesn't pre-populate profile's user attribute. This results in
> unnecessary DB queries when you go from User -> UserProfile and then
> later go from UserProfile -> User. This is actually a fairly common use
> case because people tend to attach methods that "extend" the user object
> in UserProfile, since they can't directly extend User.
>
> For example, if you create a full_name() method on UserProfile that
> returns '%s %s' % (self.user.first_name, self.user.last_name), then in
> your templates, you do {{ User.get_profile.full_name }}, you end up doing
> two DB queries when only one in necessary.
>
> The solution seems simple enough since the profile's User object must be
> called `user`. In django/contrib/auth/models.py a one liner after
> fetching the profile should do the trick:
>
> {{{
>   self._profile_cache =
> model._default_manager.get(user__id__exact=self.id)
> + self._profile_cache.user = self
> }}}

New description:

 When you use the User.get_profile() method to fetch a profile object, it
 doesn't pre-populate profile's user attribute. This results in unnecessary
 DB queries when you go from User -> `UserProfile` and then later go from
 `UserProfile` -> `User`. This is actually a fairly common use case because
 people tend to attach methods that "extend" the user object in
 UserProfile, since they can't directly extend User.

 For example, if you create a `full_name()` method on `UserProfile` that
 returns {{{'%s %s' % (self.user.first_name, self.user.last_name)}}}, then
 in your templates, you do {{{ {{ User.get_profile.full_name }} }}}, you
 end up doing two DB queries when only one in necessary.

 The solution seems simple enough since the profile's User object must be
 called `user`. In django/contrib/auth/models.py a one liner after fetching
 the profile should do the trick:

 {{{
   self._profile_cache =
 model._default_manager.get(user__id__exact=self.id)
 + self._profile_cache.user = self
 }}}

-- 
Ticket URL: <http://code.djangoproject.com/ticket/8660#comment:2>
Django Code <http://code.djangoproject.com/>
The web framework for perfectionists with deadlines
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to