Django custom user model: IntegrityError: null value in column "is_superuser" violates not-null constraint

Django custom user model: IntegrityError: null value in column "is_superuser" violates not-null constraint

我正在尝试在 Django 中创建自定义用户模型,然后根据 django-rest-auth 提供的内容创建 RESTAPI。

CustomUserModel 和 CustomUSerManager 定义为-

   from django.contrib.auth.models import (
      BaseUserManager, AbstractBaseUser
  )

class UserManager(BaseUserManager):
use_in_migrations = True

def create_user(self, email, name, phone_no, user_android_id, user_fcm_token,
                user_social_flag, user_fb_id, user_android_app_version, password=None):
    user = self.model(
        email = self.normalize_email(email),
        phone_no = phone_no,
        password=password,
        user_android_id = user_android_id,
        user_fcm_token = user_fcm_token,
        user_social_flag = user_social_flag,
        user_fb_id = user_fb_id,
        user_android_app_version = user_android_app_version,
        name = name,
    )
    # user.is_staff = False
    # user.is_superuser = True
    user.set_password(password)
    user.save(using=self._db)
    return user

def create_staffuser(self, email, name, phone_no, user_android_id, user_fcm_token,
                user_social_flag, user_fb_id, user_android_app_version, password):
    user = self.create_user(
        email,
        password=password,
        phone_no=phone_no,
        user_android_id=user_android_id,
        user_fcm_token=user_fcm_token,
        user_social_flag=user_social_flag,
        user_fb_id=user_fb_id,
        user_android_app_version=user_android_app_version,
        name=name,
    )
    user.is_staff = True
    user.is_admin = False
    user.is_superuser = True
    user.save(using=self._db)
    return user

def create_superuser(self, email, name, phone_no, user_android_id, user_fcm_token,
                user_social_flag, user_fb_id, user_android_app_version, password):
    user = self.create_user(
        email,
        password=password,
        phone_no=phone_no,
        user_android_id=user_android_id,
        user_fcm_token=user_fcm_token,
        user_social_flag=user_social_flag,
        user_fb_id=user_fb_id,
        user_android_app_version=user_android_app_version,
        name=name,
    )
    user.is_admin = True
    user.save(using=self._db)
    return user


class User(AbstractBaseUser):
   objects = UserManager()

   name = models.CharField(max_length=100, blank=True, null=True)
   email = models.EmailField(unique=True)
   created_at = models.DateField(blank=True, null=True)
   phone_no = models.BigIntegerField(blank=True, null=True)
   user_android_id = models.CharField(max_length=255, blank=True, null=True)
   user_fcm_token = models.CharField(max_length=255, blank=True, null=True)
   user_social_flag = models.IntegerField(blank=True, null=True)
   user_fb_id = models.CharField(max_length=255, blank=True, null=True)
   user_android_app_version = models.CharField(max_length=25, blank=True, null=True)

   USERNAME_FIELD = 'email'

   def __str__(self):  
      return self.email

自定义视图文件:

from rest_auth.registration.views import RegisterView
from app.models import User

class CustomRegisterView(RegisterView):
    queryset = User.objects.all()

在 settings.py 文件中我设置了:

AUTH_USER_MODEL = 'app.User'
ACCOUNT_USER_MODEL_USERNAME_FIELD = None
ACCOUNT_AUTHENTICATION_METHOD = 'email'
ACCOUNT_EMAIL_VERIFICATION = 'none'
ACCOUNT_EMAIL_REQUIRED = True
ACCOUNT_UNIQUE_EMAIL = True
ACCOUNT_USERNAME_REQUIRED = False
ACCOUNT_USER_EMAIL_FIELD = 'email'
ACCOUNT_LOGOUT_ON_GET = True


 REST_AUTH_SERIALIZERS = {
   "USER_DETAILS_SERIALIZER": 
  "app.serializers.CustomUserDetailsSerializer",
 }
REST_AUTH_REGISTER_SERIALIZERS = {
    "REGISTER_SERIALIZER": 
  "app.serializers.CustomRegisterSerializer",
 }

我已经在 url 文件中设置了 urls。

re_path(r'^registration/$', views.CustomRegisterView.as_view())

当我 运行 /registration/ url 并填写所有详细信息时,我收到错误消息:

django.db.utils.IntegrityError: null value in column "is_superuser" violates not-null constraint DETAIL: Failing row contains (8, pbkdf2_sha2560000$VpGItoUPHoJ9$yBQB2PqRDiqd4SQ2cDZX/wzaV3yFSf..., null, null, null, null, null, null, null, null, null, custom@user.com, null, null, null, null, null, null, null).

我在这里错过了什么?

更新 1:

查看数据库记录时,发现只有email、password、last_login字段只插入,其余均为null。可能是什么原因?

更新 2:

CustomRegisterSerializer class:-

   class CustomRegisterSerializer(RegisterSerializer):
    email = serializers.EmailField()
    password1 = serializers.CharField(write_only=True)
    name = serializers.CharField()
    phone_no = serializers.IntegerField()
    user_android_id = serializers.CharField()
    user_fcm_token = serializers.CharField(required=True)
    user_social_flag = serializers.IntegerField()
    user_fb_id = serializers.CharField(required=True)
    user_android_app_version = serializers.CharField()

    class Meta:
        model = User
        fields = ('email', 'name', 'phone_no', 'user_android_id', 'user_fcm_token',
                  'user_social_flag', 'user_fb_id', 'user_android_app_version')

    def get_cleaned_data(self):
        super(CustomRegisterSerializer, self).get_cleaned_data()

        return {
            'password1': self.validated_data.get('password1', ''),
            'email': self.validated_data.get('email', ''),
            'phone_no': self.validated_data.get('phone_no'),
            'name': self.validated_data.get('name'),
            'user_android_id': self.validated_data.get('user_android_id'),
            'user_fcm_token': self.validated_data.get('user_fcm_token'),
            'user_social_flag': self.validated_data.get('user_social_flag'),
            'user_fb_id': self.validated_data.get('user_fb_id'),
            'user_android_app_version': self.validated_data.get('user_android_app_version'),

        }

您在 create_user 中评论了 is_staffis_superuser。取消注释并将它们设置为 False.

编辑:

关于未进入数据库的字段 - is_staffis_superuser 未在您的用户模型中定义,也未在 AbstractBaseUser 中定义。这是 st运行ge,因为在这种情况下,您一开始就不应该得到该异常。

您是刚刚开始您的项目还是在交换用户模型之前进行了 运行 迁移? 如果您从 Django 的默认用户模型开始,您将需要做更多的工作。希望您还没有任何真正的用户,并且您可以快速完成。 仔细看看 topic in Django docs.