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?
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.
Comments