#36225: USERNAME_FIELD must be unique in order to use createsuperuser command
-------------------------------------+-------------------------------------
Reporter: Jonas Dittrich | Owner:
Type: | Abderrahmane MELEK
Cleanup/optimization | Status: assigned
Component: contrib.auth | Version: 5.1
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Sarah Boyce):
Rereading the forum post and other related ticket #35729, I agree my
initial understanding of the issue was wrong
It appears that there was a custom user which inherited `AbstractBaseUser`
with a non-unique `USERNAME` field (which we have a test for running
`createsuperuser`).
This had an issue loading fixtures created with
`use_natural_foreign_keys=True`, `use_natural_primary_keys=True` (see
#35729).
I can see how this would be problematic. If we add a model
{{{
class UserProfile(models.Model):
user = models.ForeignKey(CustomUserNonUniqueUsername,
on_delete=models.CASCADE)
}}}
An example serialized data for `UserProfile` and `AbstractBaseUser` gives
roughly:
{{{
[
{
"model": "auth_tests.customusernonuniqueusername",
"fields": {"password": "md5$***", "last_login": null, "username":
"joe", "email": "
j...@somewhere.org", "is_staff": true, "is_superuser":
true}
},
{
"model": "auth_tests.customusernonuniqueusername",
"fields": {"password": "md5$***", "last_login": null, "username":
"joe", "email": "
j...@somewhere.org", "is_staff": true, "is_superuser":
true}
},
{
"model": "auth_tests.userprofile",
"pk": 1,
"fields": {"user": ["joe"]}
}
]
}}}
`"joe"` is not a unique identifier for the `CustomUserNonUniqueUsername`
and updating this to be `pk` is still problematic as `pk` is omitted from
the serialization due to `natural_key()` being defined. So getting errors
loading this data is not surprising
Then, the advise was to not inherit `AbstractBaseUser`. The problem with
this advice is there is no docs to support how to make a valid custom user
without doing this and other hiccups happened. Specifically,
`createsuperuser` assumes a `get_by_natural_key` implementation, which
given the previous ticket, was wanted to be avoided.
Personally I am tempted to close this ticket and reopen #35729 as a bug
--
Ticket URL: <
https://code.djangoproject.com/ticket/36225#comment:10>