﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
35578	Customizing `username_validator` in custom User model extending `django.contrib.auth.models.AbstractUser` doesn't work	Oscar Rovira		"Hi there!
I'm sorry if that is the expected behaviour of what I'm going to report, but looking at the implementation, it should work as I expected.
I have my own authentication `User` model, extending from `AbstracUser`. Looking at the code of `AbstractUser`, one may think that defining the class prop `username_validator`, the validation of the username at all the levels (createsuperuser command, Admin create user form, User.objects.create_user, ...) should use the new validator provided in the customized class. It is not working like that. In order to make it work, one has to re-define also the `username` field in the customized model.

So, I have the following:
{{{
@deconstructible
class CustomUnicodeUsernameValidator(validators.RegexValidator):
    """"""Overrides the default username validator from Django Abstract
    User to avoid usernames with characters '@', '+' or '.'
    """"""
    regex = r""^[\w-]+\Z""
    message = _(
        ""Enter a valid username. This value may contain only letters, ""
        ""numbers, and -/_ characters.""
    )
    flags = 0

...
class User(AbstractUser):
    """"""Main app user, extending default Django users.""""""

    # overriding the one defined in AbstractUser
    username_validator = CustomUnicodeUsernameValidator() # type: ignore
    
    # Other custom fields
    ...
}}}

and it does not work, the custom username_validator is not being used.

However, if I redefine the `username` field, including the `validators=[username_validator]` attribute, it works as expected.
As I said, maybe it is the expected behaviour, and it's just a lack of documentation.

Thanks!"	Uncategorized	closed	contrib.auth	5.0	Normal	invalid			Unreviewed	0	0	0	0	0	0
