Opened 2 years ago

Closed 2 years ago

#20097 closed Cleanup/optimization (invalid)

The field named as the USERNAME_FIELD should not be included in REQUIRED_FIELDS on a swappable User model.

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

Description

I've created a User model that subclasses AbstractBaseUser and contains just this:

USERNAME_FIELD = 'email'

If I try to use that django gives this error:
"The field named as the USERNAME_FIELD should not be included in REQUIRED_FIELDS on a swappable User model."

Which I can't figure out because I haven't put anything in REQUIRED_FIELDS.

Change History (1)

comment:1 Changed 2 years ago by bmispelon

  • Cc bmispelon@… added
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Resolution set to invalid
  • Status changed from new to closed

Hi,

AbstractBaseUser does not have an email` field so the following is invalid:

class MyCustomUser(AbstractBaseUser):
    USERNAME_FIELD = 'email'

This will raise the following error: MyCustomUser has no field named 'email'

Now, if you subclass AbstractUser instead in the same fashion, then you get the error message you indicated.

The reason for this is that AbstractUser defines REQUIRED_FIELDS = ['email'], hence the error message.

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