Code

Opened 6 years ago

Closed 6 years ago

#7400 closed (wontfix)

Auth: get_profile() doesn't allow other name than user for foreign key

Reported by: contato@… Owned by: nobody
Component: Contrib apps Version: master
Severity: Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

Django's authentication framework get_profile() method doesn't allow another name than 'user' to foreign key field in user profile class, like:

class UserProfile(models.Model):
  another_name = models.ForeignKey(User, unique=True)

If I try to get the user profile with user.get_profile(), it generates an exception.

Error is in the get_profile method where it try to get the profile cache, getting by self._profile_cache = model._default_manager.get(user__id__exact=self.id). The problem is user__id__exact=self.id where it had to be anoter_name__id__exact=self.id or anything else.
File django/contrib/auth/models.py:

    def get_profile(self):
        """
        Returns site-specific profile for this user. Raises
        SiteProfileNotAvailable if this site does not allow profiles.
        """
        if not hasattr(self, '_profile_cache'):
            from django.conf import settings
            if not settings.AUTH_PROFILE_MODULE:
                raise SiteProfileNotAvailable
            try:
                app_label, model_name = settings.AUTH_PROFILE_MODULE.split('.')
                model = models.get_model(app_label, model_name)
                self._profile_cache = model._default_manager.get(user__id__exact=self.id) # <-- where error happens
            except (ImportError, ImproperlyConfigured):
                raise SiteProfileNotAvailable
        return self._profile_cache

Attachments (0)

Change History (1)

comment:1 Changed 6 years ago by jacob

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Resolution set to wontfix
  • Status changed from new to closed

This is just how profiles work: it's an application of duck typing. There's plenty of other ways to get from a User to some custom profile object; get_profile is for one particular common case.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.