繁体   English   中英

错误:'django.db.utils.IntegrityError: NOT NULL 约束失败:base_user.is_superuser'

[英]Error: 'django.db.utils.IntegrityError: NOT NULL constraint failed: base_user.is_superuser'

您好,我在 django 创建注册表单时出现了这个错误。
尝试了很多东西,只是来寻求帮助
这是我的代码

报名表格

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') 

用户管理器

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

我的注册视图

...
    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
...

也许错误在这里?

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

到目前为止,感谢您的关注!
我认为错误在 UserManager 中,尝试更改一些内容但总是出现相同的错误

您还应该在 create_user 时添加user_obj.is_superuser = is_superuser 它应该是 True 或 False。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM