Opened 16 years ago
Closed 16 years ago
#7400 closed (wontfix)
Auth: get_profile() doesn't allow other name than user for foreign key
Reported by: | Owned by: | nobody | |
---|---|---|---|
Component: | Contrib apps | Version: | dev |
Severity: | Keywords: | ||
Cc: | Triage Stage: | Unreviewed | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
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
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.