I have a problem with email field in django model. I have set it null True and blank True but this couldn’t prevent admin user forms or any other web forms where i include email, to not require email if sumitted blank. Seems it is related to overriden email field in my custom user model but I am new to Django, so I don’t have any idea where to pay attention to troubleshoot.
here is my custom user model with its manager..
`from django.contrib.auth.models import AbstractUser
from django.db import models
from django.utils.translation import gettext_lazy as _
from .managers import UserManager
class User(AbstractUser):
GENDER_CHOICES = (
('male', _('Male')),
('female', _('Female')),
('not_set', _('Not set')),
)
email = models.CharField(_('Email'), max_length=30, unique=True, blank=True, null=True)
phone = models.PositiveBigIntegerField(_('Phone number'), unique=True, null=True, blank=True)
telegram = models.BigIntegerField(_('Telegram ID'), unique=True, null=True, blank=True)
first_name = models.CharField(_('First name'), max_length=50, blank=True, null=True )
last_name = models.CharField(_('Last name'), max_length=50, blank=True, null=True )
middle_name = models.CharField(_('Middle name'), max_length=50, blank=True, null=True )
gender = models.CharField(_('Gender'), max_length=7, choices=GENDER_CHOICES, default='not_set')
profile_image = models.ImageField(_("Profile image"), upload_to='user/img/profile/', blank=True, null=True)
cover_image = models.ImageField(_("Cover image"), upload_to='user/img/cover/', blank=True, null=True)
user_verified_at = models.DateTimeField(_('User verified at'), null=True, blank=True)
phone_verified_at = models.DateTimeField(_('Phone verified at'), null=True, blank=True)
telegram_linked_at = models.DateTimeField(_('Telegram linked at'), null=True, blank=True)
USERNAME_FIELD = 'username'
REQUIRED_FIELDS = []
objects = UserManager()
class Meta:
verbose_name = _("User")
verbose_name_plural = _("Users")
def str(self):
return str(self.username)
class ReservedUsername(models.Model):
username = models.CharField(max_length=30, unique=True)
def str(self):
return self.username
class Meta:
verbose_name = _("Reserved Username")
verbose_name_plural = _("Reserved Usernames")`
managers.py
`
from django.contrib.auth.base_user import BaseUserManager
class UserManager(BaseUserManager):
use_in_migrations = True
def _create_user(self, username, password, **extra_fields):
if not username:
raise ValueError('Username is required')
user = self.model(username=username, **extra_fields)
user.set_password(password)
user.save(using=self._db)
return user
def create_user(self, username, password=None, **extra_fields):
extra_fields.setdefault('is_staff', False)
extra_fields.setdefault('is_superuser', False)
extra_fields.setdefault('is_active', True)
return self._create_user(username, password, **extra_fields)
def create_superuser(self, username, password, **extra_fields):
extra_fields.setdefault('is_staff', True)
extra_fields.setdefault('is_superuser', True)
extra_fields.setdefault('is_active', True)
if extra_fields.get('is_staff') is not True:
raise ValueError('Superuser must be a staff')
if extra_fields.get('is_superuser') is not True:
raise ValueError('Superuser must be a superuser')
return self._create_user(username, password, **extra_fields)`
one more thing.. if you could help, i need to make managers.py more efficient. I recently noticed any validation for username length, char regexp i configure there is not taking affect anywhere in project.
Thanks for answer to anyone in advance.
I tried to set email field null=True, blank=True
2
Answers
Double check your forms; there may be some sort of validation on the form level which is getting caught up. Also, you might be better off using EmailField rather than CharField. This gives access to the EmailValidator.
Problem :-
I think it’s because unique=True.
So two people can’t put a blank email, that’s because both values will be equal (”=”).
Like, it become
empty = empty
.Answers :-
I would say,
1)
to remove
unique=True
and use a DB constrain to keep the uniqueness and allow empty values.If you really want to use
CharField
for this.Or
2)
Use
EmailField
and removedunique=True
. Which is more convenient, suitable and which is build for this.And make sure to delete old DB migrations and run makemigrations and migrate.
Thanks.