Home > Software engineering >  Error: 'django.db.utils.IntegrityError: NOT NULL constraint failed: base_user.is_superuser'
Error: 'django.db.utils.IntegrityError: NOT NULL constraint failed: base_user.is_superuser'

Time:02-26

Hi, I'm creating a registration form in django and this error occured.
Tried many things and just came for help
Here's my code

RegistrationForm

class RegistrationForm(UserCreationForm):
    full_name = forms.CharField(max_length=255, widget=forms.TextInput(attrs={'class': 'inpt', 'placeholder': 'Insira o seu nome completo'}))
    email = forms.EmailField(max_length=255, help_text="Required email", widget=forms.EmailInput(attrs={'class': 'inpt', 'placeholder': 'Insira o seu email'}))
    password1 = forms.CharField(max_length=255, widget=forms.PasswordInput(attrs={'class': 'inpt', 'placeholder': 'Insira a sua password'}))
    password2 = forms.CharField(max_length=255, widget=forms.PasswordInput(attrs={'class': 'inpt', 'placeholder': 'Confirme a sua password'}))

    class Meta:
        model = User
        fields = ('full_name', 'email', 'password1', 'password2') 

UserManager

class UserManager(BaseUserManager):
    def create_user(self, email, full_name, password=None, is_staff=False, is_admin=False, is_superuser=False):
        if not email:
            raise ValueError('User must have an email address')
        if not password:
            raise ValueError('Users must have a password')
        user_obj = self.model(
            email = self.normalize_email(email)
        )
        user_obj.set_password(password)
        user_obj.staff = is_staff
        user_obj.admin = is_admin
        user_obj.full_name = full_name
        user_obj.save(using=self.db)
        
        return user_obj
    
    def create_superuser(self, email, full_name, password=None):
        user = self.create_user(
            email,
            password=password,
            full_name=full_name,
            is_staff=True,
            is_admin=True,
            is_superuser=True        
        )
        return user

My register view

...
    context = {}
    if request.method == 'POST':
        form = RegistrationForm(request.POST)
        if form.is_valid():
            form.save()
            email = form.cleaned_data.get('email')
            raw_password = form.cleaned_data.get('password1')
            user = authenticate(email=email, password=raw_password)
            login(request, user)
            return redirect('home')
        else:
            context['registration_form'] = form
    else:
        form = RegistrationForm()
        context['registration_form'] = form
...

Maybe the error is here?

class User(AbstractBaseUser):
    full_name = models.CharField(max_length=255)
    email = models.EmailField(max_length=255, unique=True)
    staff = models.BooleanField(default=False)
    admin = models.BooleanField(default=False)
    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = ['full_name', 'password',]

    objects = UserManager()

    def __str__(self):
        return self.email

    def get_full_name(self):
        return self.full_name

    def get_first_last_name(self):
        short_name = self.full_name.split()
        return short_name[0]   ' '   short_name[-1]

    def get_short_name(self):
        short_name = self.full_name.split()
        return short_name[0] 

    def has_perm(self, perm, obj=None):
        return True

    def has_module_perms(self, app_label):
        return True

    @property
    def is_staff(self):
        return self.staff

    @property
    def is_admin(self):
        return self.admin

By far thank you for your attention!
I think the error is in the UserManager, tried changing some things but same error always

CodePudding user response:

You should also add user_obj.is_superuser = is_superuser while create_user. It should be either True or False.

  • Related