Code

Opened 3 years ago

Closed 12 months ago

#15701 closed Bug (wontfix)

User._profile_cache cache collides when a model uses "profile" as related_name to User

Reported by: dgouldin Owned by: nobody
Component: contrib.auth Version: 1.2
Severity: Normal Keywords:
Cc: dgouldin@… Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Consider the following models in accounts/models.py

from django.contrib.auth.models import User
from django.db import models

class UserProfile(models.Model):
    user = models.OneToOneField(User, related_name="real_profile")

class NotUserProfile(models.Model):
    user = models.OneToOneField(User, related_name="profile")

With a setting in settings.py

AUTH_PROFILE_MODULE = 'accounts.UserProfile'

An orm call to User's manager with a select_related on "profile" will give the wrong answer for user.get_profile()

In [8]: user = User.objects.select_related('profile').all()[0]

In [9]: user.get_profile()
Out[9]: <NotUserProfile: NotUserProfile object>

This is due to a property name collision between User.get_profile

def get_profile(self):
    if not hasattr(self, '_profile_cache'):
        # ...
    return self._profile_cache

and RelatedObject

def get_cache_name(self):
    return "_%s_cache" % self.get_accessor_name()

Attachments (0)

Change History (9)

comment:1 Changed 3 years ago by dgouldin

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

Whoops, title should obviously ready "User._profile_cache ...". Had it been named as the title suggests, the problem wouldn't have occurred. ;-)

comment:2 Changed 3 years ago by dgouldin

  • Summary changed from User._cached_profile cache collides when a model uses "profile" as related_name to User to User._profile_cache cache collides when a model uses "profile" as related_name to User

comment:3 Changed 3 years ago by Alex

  • Component changed from Database layer (models, ORM) to Authentication
  • Triage Stage changed from Unreviewed to Accepted

comment:4 Changed 3 years ago by adrian

So is the solution to improve our error validation such that it checks for this name clash and forbids it?

comment:5 Changed 3 years ago by lukeplant

  • Type set to Bug

comment:6 Changed 3 years ago by lukeplant

  • Severity set to Normal

comment:7 Changed 2 years ago by aaugustin

  • UI/UX unset

Change UI/UX from NULL to False.

comment:8 Changed 2 years ago by aaugustin

  • Easy pickings unset

Change Easy pickings from NULL to False.

comment:9 Changed 12 months ago by timo

  • Resolution set to wontfix
  • Status changed from new to closed

Marking as won't fix since AUTH_PROFILE_MODULE is deprecated.

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.