我试图在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",
}
我已经在网址文件中设置了网址。
re_path(r'^registration/$', views.CustomRegisterView.as_view())
当我运行/ registration /网址并填写所有详细信息时,我会收到错误消息:
django.db.utils.IntegrityError:“ is_superuser”列中的空值违反了非空约束 详细信息:失败行包含(8,pbkdf2_sha256 $ 120000 $ VpGItoUPHoJ9 $ yBQB2PqRDiqd4SQ2cDZX / wzaV3yFSf ...,null,null,null,null,null,null,null,null,null,null,null,custom@user.com,null,null,null,null, null,null,null,null)。
我在这里想念什么?
更新1:
在检查数据库记录时,我发现仅电子邮件,密码和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'),
}
答案 0 :(得分:3)
在create_user
中,您评论了is_staff
和is_superuser
。取消注释并将它们设置为False
。
编辑:
关于字段未进入数据库-is_staff
和is_superuser
在用户模型中未定义,在AbstractBaseUser
中也未定义。这很奇怪,因为在这种情况下,您不应该首先获得该异常。
您是刚开始项目还是在交换用户模型之前进行了迁移? 如果您从Django的默认用户模型开始,则需要做更多的工作。希望您还没有真正的用户,并且可以快速完成。 请仔细查看topic in Django docs。