Opened 14 years ago

Closed 11 years ago

#15701 closed Bug (wontfix)

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

Reported by: David Gouldin 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()

Change History (9)

comment:1 by David Gouldin, 14 years ago

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

comment:2 by David Gouldin, 14 years ago

Summary: User._cached_profile cache collides when a model uses "profile" as related_name to UserUser._profile_cache cache collides when a model uses "profile" as related_name to User

comment:3 by Alex Gaynor, 14 years ago

Component: Database layer (models, ORM)Authentication
Triage Stage: UnreviewedAccepted

comment:4 by Adrian Holovaty, 14 years ago

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

comment:5 by Luke Plant, 14 years ago

Type: Bug

comment:6 by Luke Plant, 14 years ago

Severity: Normal

comment:7 by Aymeric Augustin, 13 years ago

UI/UX: unset

Change UI/UX from NULL to False.

comment:8 by Aymeric Augustin, 13 years ago

Easy pickings: unset

Change Easy pickings from NULL to False.

comment:9 by Tim Graham, 11 years ago

Resolution: wontfix
Status: newclosed

Marking as won't fix since AUTH_PROFILE_MODULE is deprecated.

Note: See TracTickets for help on using tickets.
Back to Top