Issue
This is custom user model class
class User(AbstractUser, BaseUserManager):
username=None
name = models.CharField(max_length=255)
created_by =models.ForeignKey('self',on_delete=models.CASCADE,null=True,blank=True)
email = models.EmailField(unique=True)
password = models.CharField(max_length=128)
is_active = models.BooleanField(default=True)
is_staff = models.BooleanField(default=False)
is_admin = models.BooleanField(default=False)
is_verified = models.BooleanField(default=False)
is_superuser= models.BooleanField(default=False)
date_joined = models.DateTimeField(auto_now_add=True)
last_login = models.DateTimeField()
USERNAME_FIELD = 'email'
REQUIRED_FIELDS=['password']
objects = ContentManager()
def __str__(self):
return self.email
This is my manager.py
for User model
class ContentManager(BaseUserManager):
use_in_migrations=True
def create_user(self, email,password, **extra_fields):
if not email:
raise ValueError("Email must be set")
email = self.normalize_email(email)
user = self.model(email=email,**extra_fields)
user.set_password(password)
user.save(using=self.db)
return user
def create_super_user (self,email, password,name, **extra_fields):
extra_fields.setdefault('is_staff',True)
extra_fields.setdefault('is_admin',True)
extra_fields.setdefault('is_superuser',True)
return self.create_user(email,password,**extra_fields)
The problem is when I wanted to create the user from django shell by following method.
python manage.py shell
from api.models import Users
user = User.objects.create_user(name="roshan",email="[email protected]",password="password")
Even if put some data in name
field, it is showing django.db.utils.IntegrityError: (1048, "Column 'name' cannot be null")
error.
I couldn't figure out where I went wrong. Please help.
Solution
Your User
class should not inherit from BaseUserManager
(only the manager should), E.g.,
class User(AbstractUser):
See Substituting a custom User model in the django documentation for more details.
With class User(AbstractUser, BaseUserManager)
, eventually the name was being overwritten (by BaseManager
).
Answered By - DanielB
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.