[Django]-Create and use a custom username validator for a registration form

5👍

Just create your own User model just like that:

class User(AbstractUser):
    username = models.CharField(
        max_length=150,
        unique=True,
        validators=[AlphaNumericUsernameValidator],
        error_messages={
            'unique': _("A user with that username already exists."),
        },
    )

Don’t forget to define it in settings.py in AUTH_USER_MODEL variable.

Also, you probably need another regexp

regex = r'^[\w][\w\d_]+$'
👤bugov

Leave a comment