Opened 4 years ago

Closed 4 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 4 years ago by Anssi Kääriäinen

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 4 years ago by Ivan Virabyan

Resolution: invalid
Status: newclosed

comment:3 Changed 4 years ago by Ivan Virabyan

I'm sorry. I have misspelled setting name.

Last edited 4 years ago by Ivan Virabyan (previous) (diff)

comment:4 Changed 4 years ago by Ivan Virabyan

Resolution: invalid
Status: closedreopened

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

Version 0, edited 4 years ago by Ivan Virabyan (next)

comment:5 Changed 4 years ago by Preston Holmes

Triage Stage: UnreviewedAccepted

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 4 years ago by Russell Keith-Magee

Severity: NormalRelease 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 4 years ago by Russell Keith-Magee <russell@…>

Resolution: fixed
Status: reopenedclosed

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 4 years ago by Preston Holmes

Resolution: fixed
Status: closedreopened

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 4 years ago by Russell Keith-Magee <russell@…>

Resolution: fixed
Status: reopenedclosed

In 43530384b7f2a18b44e06e7043b988427b334653:

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

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