0

我正在尝试在 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 文件中设置了 url。

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

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

django.db.utils.IntegrityError:“is_superuser”列中的空值违反非空约束细节:失败行包含(8,pbkdf2_sha256$120000$VpGItoUPHoJ9$yBQB2PqRDiqd4SQ2cDZX/wzaV3yFSf...,null,null,null,null,null,空,空,空,空,custom@user.com,空,空,空,空,空,空,空)。

我在这里想念什么?

更新 1:

在检查数据库记录时,我发现只插入了 email、password 和 last_login 字段,其余字段均为空。可能是什么原因?

更新 2:

CustomRegisterSerializer 类:-

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

        }
4

1 回答 1

3

create_user你评论is_staffis_superuser。取消注释并将它们设置为False.

编辑:

关于未进入数据库的字段 -未在您的用户模型中定义,也未is_staff在. 这很奇怪,因为在这种情况下,您一开始就不应该遇到该异常。is_superuserAbstractBaseUser

您是刚刚开始您的项目还是在交换用户模型之前运行了迁移?如果你从 Django 的默认用户模型开始,你需要做更多的工作。希望您还没有任何真正的用户,您可以快速完成。仔细查看Django 文档中的主题

于 2018-11-14T12:59:35.883 回答