Opened 2 years ago

Closed 2 years ago

#19049 closed Bug (fixed)

Subclassing AbstractUser doesn't work

Reported by: ivan_virabyan Owned by: nobody
Component: contrib.auth Version: master
Severity: Release blocker Keywords:
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

According to https://docs.djangoproject.com/en/dev/topics/auth/#extending-django-s-default-user I created my own User model

from django.contrib.auth.models import AbstractUser

class User(AbstractUser):
    avatar = models.ImageField(upload_to='avatars')

but django complains:

CommandError: One or more models did not validate:
accounts.user: Accessor for m2m field 'groups' clashes with related m2m field 'Group.user_set'. Add a related_name argument to the definition for 'groups'.
accounts.user: Accessor for m2m field 'user_permissions' clashes with related m2m field 'Permission.user_set'. Add a related_name argument to the definition for 'user_permissions'.
auth.user: Accessor for m2m field 'groups' clashes with related m2m field 'Group.user_set'. Add a related_name argument to the definition for 'groups'.
auth.user: Accessor for m2m field 'user_permissions' clashes with related m2m field 'Permission.user_set'. Add a related_name argument to the definition for 'user_permissions'.

Change History (9)

comment:1 Changed 2 years ago by akaariai

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

Do you have settings.AUTH_USER_MODEL set to your User model? It seems you have both auth.user and accounts.user installed... I don't think it is possible to have subclassed AbstractUser in use in addition to the default User model.

comment:2 Changed 2 years ago by ivan_virabyan

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

comment:3 Changed 2 years ago by ivan_virabyan

I'm sorry. I have misspelled setting name.

Last edited 2 years ago by ivan_virabyan (previous) (diff)

comment:4 Changed 2 years ago by ivan_virabyan

  • Resolution invalid deleted
  • Status changed from closed to reopened

Nevertheless, django still complains. It only work if I rename my model to CustomUser or something.

Version 0, edited 2 years ago by ivan_virabyan (next)

comment:5 Changed 2 years ago by ptone

  • Triage Stage changed from Unreviewed to Accepted

this is because django.contrib.auth.models.User and group are being installed as loaded models, and reverse relations are being set to Django's default 'User' model, and reserving the user_set etc reverse relations

comment:6 Changed 2 years ago by russellm

  • Severity changed from Normal to Release blocker

This will need to be fixed before release; banning "User" as a model name is a pretty big failing, and there's also the possibility that stray reverse relations are being installed.

comment:7 Changed 2 years ago by Russell Keith-Magee <russell@…>

  • Resolution set to fixed
  • Status changed from reopened to closed

In 3b6f980bedbbf091fe29bececa2b262d2084ce4d:

Fixed #19049 -- Ensure that swapped models aren't included in reverse field caches.

Thanks to Ivan Virabyan for the report.

comment:8 Changed 2 years ago by ptone

  • Resolution fixed deleted
  • Status changed from closed to reopened

while this fixes the auth tests - it badly blows up everything else it seems

http://ci.djangoproject.com/job/Django/database=sqlite3,python=python2.7/1763/

comment:9 Changed 2 years ago by Russell Keith-Magee <russell@…>

  • Resolution set to fixed
  • Status changed from reopened to closed

In 43530384b7f2a18b44e06e7043b988427b334653:

Fixed #19049 -- Corrected dumb logic negation error from earlier patch.

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