[英]TypeError: User() got an unexpected keyword argument 'confirm_password'
I want to add password
field and confirm_password
field to my UserSerializer
.我想将
password
字段和UserSerializer
confirm_password
I wrote a function called create
to create a hashes password for my password
field but before it can create the hashes password I want it to make sure that confirm_password
and password
are matched.我写了一个名为
create
的confirm_password
来为我的password
字段创建一个哈希密码,但在它可以创建哈希密码之前,我希望它确保确认密码和password
匹配。 The code works fine if I remove the confirm_password
field.如果我删除了
confirm_password
字段,代码就可以正常工作。 What is the problem?问题是什么?
[ Updated ] [ 更新 ]
My serializers.py我的序列化程序.py
# serializer define the API representation
class UserSerializer(serializers.HyperlinkedModelSerializer):
# password field
password = serializers.CharField(
write_only = True,
required = True,
help_text = 'Enter password',
style = {'input_type': 'password'}
)
# confirm password field
confirm_password = serializers.CharField(
write_only = True,
required = True,
help_text = 'Enter confirm password',
style = {'input_type': 'password'}
)
class Meta:
model = User
fields = [
'url', 'first_name', 'last_name', 'email',
'password', 'confirm_password', 'is_staff'
]
def create(self, validated_data):
if validated_data.get('password') != validated_data.get('confirm_password'):
raise serializers.ValidationError("Those password don't match")
elif validated_data.get('password') == validated_data.get('confirm_password'):
validated_data['password'] = make_password(
validated_data.get('password')
)
return super(UserSerializer, self).create(validated_data)
error I got我得到的错误
TypeError: User() got an unexpected keyword argument 'confirm_password'
[20/Aug/2020 16:15:44] "POST /users/ HTTP/1.1" 500 168152
error I got in browser我在浏览器中遇到的错误
TypeError at /users/
Got a `TypeError` when calling `User.objects.create()`. This may be because you have a writable field on the serializer class that is not a valid argument to `User.objects.create()`. You may need to make the field read-only, or override the UserSerializer.create() method to handle this correctly.
I can edit the question if you need more detail.如果您需要更多详细信息,我可以编辑问题。 Ty!
泰!
You are trying to save field confirm_password
into your User
model.您正在尝试将字段
confirm_password
保存到您的User
model 中。 I believe that this field is used only to confirm password, but User
model doesn't have this field really.我相信该字段仅用于确认密码,但
User
model 确实没有此字段。
Try to pop this field from validated_data
before saving:在保存之前尝试从
validated_data
数据中弹出该字段:
def create(self, validated_data):
if validated_data.get('password') != validated_data.get('confirm_password'):
raise serializers.ValidationError("Those password don't match")
elif validated_data.get('password') == validated_data.get('confirm_password'):
validated_data['password'] = make_password(
validated_data.get('password')
)
validated_data.pop('confirm_password') # add this
return super(UserSerializer, self).create(validated_data)
PS Validation is usually done in validate()
method, not in create()
. PS 验证通常在
validate()
方法中完成,而不是在create()
中。
Try using password1
and password2
instead of password
and confirm_passowrd
as password field names, respectively.尝试分别使用
password1
和password2
代替password
和confirm_passowrd
作为密码字段名称。
You are performing validation in create
method.您正在
create
方法中执行验证。 This is not right approach.这不是正确的做法。 This should be done in
is_valid
or validate
method of serializer.这应该在序列化程序的
is_valid
或validate
方法中完成。 Your create
method should look like this您的
create
方法应如下所示
def create(self, validated_data):
# confirm_password should not be sent to create as it is not part of User model
validated_data.pop('confirm_password', None)
return super(UserSerializer, self).create(validated_data)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.