How to validate custom user model username to prevent blank spaces

Baykay Young

Here's my code:

class User(AbstractBaseUser, PermissionsMixin):
    email = models.EmailField(
        verbose_name=_('email address'), max_length=255, unique=True
    )
    username = models.CharField(_('username'), max_length=30, unique=True)
    first_name = models.CharField(_('first name'), max_length=30, blank=True)
    last_name = models.CharField(_('last name'), max_length=150, blank=True)

How do I prevent users from using blank spaces in the username field?

solarissmoke

You can use the UnicodeUsernameValidator validator that Django itself uses:

from django.contrib.auth.validators import UnicodeUsernameValidator

username = models.CharField(
    _('username'), max_length=30, unique=True, 
    validators=[UnicodeUsernameValidator()]
)

You could, depending on your use case, also subclass AbstractUser to avoid having to redefine some fields.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to prevent a textarea to insert extra blank spaces?

How to change the field of username to user_name in to django custom user model?

mongoose how to prevent user already with username to modify username

Login user for a custom user model with email as username field

Display username in template with custom user model as welcome message

how to validate username when creating new user in angular?

How to associate a username from the User model to another model in Django?

How to validate username in PHP

Django custom User model throwing SystemCheckError - The field 'username' clashes with the name 'username'

authenticate(request, username=username, password =pswd) returns None for custom user model

How to validate a string with spaces?

How to prevent a user from just typing spaces in a textarea

How to prevent user from entering spaces in an input form field in ReactJS?

How to prevent 'no username' in django?

How do I add username of logged in user to model field in django

How to show username from User model to the index view of Quote controller?

How can I get a username from django User Model

Custom User Model Django TypeError: create_user() missing 1 required positional argument: 'username'

How to validate username with regular expression

how to make a regex to validate a username

Flutter How to validate uniqe username?

How to validate and update the model when user types in UItextfields in swift

How to get message to user from before_validate in the model

Auth_User - Leaving the Username field Blank

How to change manager for custom User model

How to make custom authentication not based on User model

How to serialize custom user model in DRF

How to add custom user model in admin panel?

How to select a custom User model property?

TOP Ranking

HotTag

Archive