django.db.utils.IntegrityError: UNIQUE constraint failed: users_newuser.user_name

models.py


from django.db import models
from django.utils import timezone
from django.utils.translation import gettext_lazy as _
from django.contrib.auth.models import AbstractBaseUser, PermissionsMixin, BaseUserManager


class CustomAccountManager(BaseUserManager):

    def create_superuser(self, email, user_name, first_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(
                'Superuser must be assigned to is_staff=True.')
        if other_fields.get('is_superuser') is not True:
            raise ValueError(
                'Superuser must be assigned to is_superuser=True.')

        return self.create_user(email, user_name, first_name, password, **other_fields)

    def create_user(self, email, user_name, first_name, password, **other_fields):

        if not email:
            raise ValueError(_('You must provide an email address'))

        email = self.normalize_email(email)
        user = self.model(email=email, user_name=user_name,
                          first_name=first_name, **other_fields)
        user.set_password(password)
        user.save()
        return user


class NewUser(AbstractBaseUser, PermissionsMixin):

    email = models.EmailField(_('email address'), unique=True)
    user_name = models.CharField(max_length=150, unique=True,blank=True)
    first_name = models.CharField(max_length=150, blank=True)
    start_date = models.DateTimeField(default=timezone.now)
    about = models.TextField(_(
        'about'), max_length=500, blank=True)
    is_staff = models.BooleanField(default=False)
    is_active = models.BooleanField(default=False)

    objects = CustomAccountManager()

    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = [ 'first_name']

    def __str__(self):
        return self.user_name

forms.py


from django.contrib.auth.forms import UserCreationForm, UserChangeForm

from .models import NewUser


class CustomUserCreationForm(UserCreationForm):

    class Meta:
        model = NewUser
        fields = ('first_name','email',)


class CustomUserChangeForm(UserChangeForm):

    class Meta:
        model = NewUser
        fields = ('first_name','email',)

How to fix this problem?

Your model has:

    user_name = models.CharField(max_length=150, unique=True,blank=True)

unique=True means “define a unique constraint for this field”: Model field reference | Django documentation | Django

To help you solve why you’re seeing this problem, please provide more context

1 Like

@NobinPegasus Did you ever manage to solve this?? I have the same problem.

If you are getting a Unique constraint failed error, I would suggest you open up a new topic for this discussion, along with providing all the relevent details.

1 Like