#35578: Customizing `username_validator` in custom User model extending
`django.contrib.auth.models.AbstractUser` doesn't work
-------------------------------+--------------------------------------
Reporter: Oscar Rovira | Owner: (none)
Type: Uncategorized | Status: closed
Component: contrib.auth | Version: 5.0
Severity: Normal | Resolution: invalid
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------
Changes (by Natalia Bidart):
* resolution: => invalid
* status: new => closed
Comment:
Hello Oscar, thank you for your report. What you are describing is
explained by how Python class definition works, this is not about Django.
See this simpler example:
{{{#!python
In [1]: class Abstract:
...: validator = "Foo"
...: username = f"The {validator} username"
In [2]: Abstract().username
Out[2]: 'The Foo username'
In [3]: class MyClass(Abstract):
...: validator = "Bar"
In [4]: MyClass().username
Out[4]: 'The Foo username'
}}}
In this example, the class attribute `username` is defined (and
"calculated") at class definition time and is not something "dynamic" that
you can expect it to be "recalculated" in children, if you don't override
it.
Having said the above, please also note that
[
https://docs.djangoproject.com/en/dev/topics/auth/customizing/ the user
customization docs] indicate that if you wish to customize the `User`
model, ideally you would extend `AbstractBaseUser`.
[
https://docs.djangoproject.com/en/dev/topics/auth/customizing/#extending-
django-s-default-user Specifically AbstractUser should be used when adding
more fiels, not changing existing fields behavior]:
> If you’re entirely happy with Django’s User model, but you want to add
some additional profile information, you could subclass
django.contrib.auth.models.AbstractUser and add your custom profile
fields, although we’d recommend a separate model as described in
Specifying a custom user model.
So this means that if you are extending `AbstractUser`, the default
username validation should suffice for your case. If it does not suffice,
then perhaps you should consider extending `AbstractBaseUser` and building
your own validator and custom username field as your business logic
requires.
Because of the above, this report seems better suited to be a support
request. The best place to get more answers to your issue is using any of
the user support channels from
[
https://docs.djangoproject.com/en/dev/faq/help/#how-do-i-do-x-why-
doesn-t-y-work-where-can-i-go-to-get-help this link]. Since the goal of
this issue tracker is to track issues about Django itself, and your issue
seems, at first, to be located in your custom code, I'll be closing this
ticket as invalid following the
[
https://docs.djangoproject.com/en/dev/internals/contributing/triaging-
tickets/#closing-tickets ticket triaging process].
--
Ticket URL: <
https://code.djangoproject.com/ticket/35578#comment:1>