我试图在django中创建一个自定义用户模型。我得到的错误是
如果不是isinstance(cls.required_字段,(列表,元组)):attributeerror:type对象“customuser”没有属性“required_字段”
我在settings.py中添加了我的customuser模型,如下所示。
AUTH_USER_MODEL = "Pollapp.CustomUser"
这是我的model.py
from django.contrib.auth.models import BaseUserManager, PermissionsMixin
from django.contrib.auth.base_user import AbstractBaseUser
class UserManager(BaseUserManager):
def create_user(self, email, firstname, lastname, phone,password=None):
user = self.model(
email = self.normalize_email(email),
firstname = firstname,
lastname = lastname,
phone = phone,
)
user.set_password(password)
user.save(using = self._db)
return user
def create_superuser(self, email,firstname,lastname,phone,password=None):
user = self.create_user(
email=email,
password=password,
firstname = firstname,
lastname = lastname,
phone = phone,
)
user.is_admin = True
user.is_staff = True
user.save(using=self._db)
return user
class CustomUser(AbstractBaseUser, PermissionsMixin):
id = models.CharField(max_length=200, default=uuid.uuid4,unique=True,primary_key=True)
email = models.EmailField(null=False, max_length=100,unique=True)
firstname = models.CharField(null=False, max_length=100)
lastname = models.CharField(null=False, max_length=100)
phone = models.IntegerField(null=False,unique=True)
date_joined = models.DateTimeField(auto_now=True)
last_login = models.DateTimeField(auto_now=True)
is_admin = models.BooleanField(default = False)
is_active = models.BooleanField(default = True)
is_staff = models.BooleanField(default = False)
is_superuser = models.BooleanField(default = False)
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['firstname','lastname','phone']
objects = UserManager()
def __str__(self):
return self.email + ", " + self.firstname
def has_perm(self, perm, obj = None):
return self.is_admin
def has_module_perms(self, app_label):
return True
我还尝试提供必需的字段,如必需的字段=('firstname'、'lastname'、'phone')
这是我的serializers.py
class UserSerializer(serializers.ModelSerializer):
class Meta:
model = CustomUser
fields = ('__all__')
class UserSerializerWithToken(serializers.ModelSerializer):
token = serializers.SerializerMethodField()
password = serializers.CharField(write_only=True)
def get_token(self, obj):
jwt_payload_handler = api_settings.JWT_PAYLOAD_HANDLER
jwt_encode_handler = api_settings.JWT_ENCODE_HANDLER
payload = jwt_payload_handler(obj)
token = jwt_encode_handler(payload)
return token
def create(self, validated_data):
password = validated_data.pop('password', None)
instance = self.Meta.model(**validated_data)
if password is not None:
instance.set_password(password)
instance.save()
return instance
class Meta:
model = CustomUser
fields = ('token', 'username', 'password')
class RegisterSerializer(serializers.ModelSerializer):
password = serializers.CharField(write_only=True, required=True, validators=[validate_password])
password2 = serializers.CharField(write_only=True, required=True)
class Meta:
model = CustomUser
fields = ('username', 'password', 'password2', 'email', 'first_name', 'last_name')
def validate(self, attrs):
if attrs['password'] != attrs['password2']:
raise serializers.ValidationError({"password": "Password fields didn't match."})
return attrs
def create(self, validated_data):
user = User.objects.create(
username=validated_data['username'],
email=validated_data['email'],
first_name=validated_data['first_name'],
last_name=validated_data['last_name'],
)
user.set_password(validated_data['password'])
user.save()
return user
暂无答案!
目前还没有任何答案,快来回答吧!