'Django:IntegrityError: UNIQUE constraint failed: backend_newuser.email
I made custom user model in django. After creating one superuser succesfully I am unable to create new super user, it gives the error above, I don't understand the reason behind this.
models.py
from django.db import models
from django.utils import timezone
from django.contrib.auth.models import AbstractBaseUser,PermissionsMixin, BaseUserManager
class CustomUserManager(BaseUserManager,):
def create_user(self,email,password,user_name,about,**other_fields):
email=self.normalize_email(str(email)); #for example ignore the case sensitivity
user=self.model(email=email,user_name=user_name,about=about)
user.set_password(password)
user.save()
return user;
def create_superuser(self, email,user_name,password,**other_fields):
other_fields.setdefault('is_staff',True)
other_fields.setdefault('is_superuser',True)
other_fields.setdefault('is_active',True)
if other_fields.get('is_staff') is not True:
raise ValueError('super user must be assigned to is_staff= True')
if other_fields.get('is_superuser') is not True:
raise ValueError('Super user must be assigned to is_super_user=True')
return self.create_user (self,email,user_name,password,**other_fields)
class NewUser(AbstractBaseUser,PermissionsMixin):
email=models.EmailField(max_length=255,unique=True)
user_name=models.CharField(max_length=32,unique=True)
about =models.CharField(max_length=512,blank=True)
is_staff=models.BooleanField(default=False)
is_active=models.BooleanField(default=False)
objects=CustomUserManager();
USERNAME_FIELD='email'
REQUIRED_FIELDS=['user_name']
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
