Opened 5 years ago

Closed 5 years ago

#20657 closed Cleanup/optimization (duplicate)

Have UserChangeForm and UserCreationForm utilize get_user_model()

Reported by: ChocolateCookies Owned by: nobody
Component: Uncategorized Version: 1.5
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


While I was trying to re-use the creation form for the admin interface with my custom User model (derived from AbstractUser), I stumbled upon an issue, namely I couldn't re-use it. I might have thought of a solution, but it's probably not very elegant.

In contrib.auth.forms the model meta property for UserChangeForm is User, but in for example AuthenticationForm, the method get_user_model is utilized. Consequently I cannot use the form, because this class imports auth.User instead of my own User model. Is this on purpose, or would it be possible to extend AUTH_USER_MODEL to the creation and change forms?

I would say it is only possible if a custom User model is derived from AbstractUser, as opposed to AbstractBaseUser (Like the Django docs suggest). AbstractUser guarantees the fields that these forms use (username, email), while AbstractBaseUser doesn't. Although that takes away a lot of the flexibility of specifying a custom User model, so maybe there's a middle ground somewhere.


class UserChangeForm(forms.ModelForm):
    class Meta:
        model = User

Change History (1)

comment:1 Changed 5 years ago by Baptiste Mispelon

Resolution: duplicate
Status: newclosed

Correct me if I'm wrong, but I believe this is a duplicate of #19353.

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