简体   繁体   English

TypeError: User() 得到了一个意外的关键字参数“confirm_password”

[英]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.我写了一个名为createconfirm_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()中。

from_the_docs . 来自_the_docs

Try using password1 and password2 instead of password and confirm_passowrd as password field names, respectively.尝试分别使用password1password2代替passwordconfirm_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_validvalidate方法中完成。 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.

相关问题 “ confirm_password”是此函数的无效关键字参数 - 'confirm_password' is an invalid keyword argument for this function TypeError:得到一个意外的关键字参数 - TypeError: got an unexpected keyword argument Flask: TypeError: blog() got an unexpected keyword argument 'user' - Flask: TypeError: blog() got an unexpected keyword argument 'user' 类型错误:User() 得到了一个意外的关键字参数“is_staff” - TypeError: User() got an unexpected keyword argument 'is_staff' 收到了意外的关键字参数“用户” - got an unexpected keyword argument 'user' 类型错误:function() 得到了一个意外的关键字参数“njobs” - TypeError: function() got an unexpected keyword argument 'njobs' 类型错误:_log() 得到了意外的关键字参数“stacklevel” - TypeError: _log() got an unexpected keyword argument 'stacklevel' 类型错误:有一个意外的关键字参数“条目” - TypeError: got an unexpected keyword argument 'entry' TypeError:editProfile()得到了意外的关键字参数“ obj” - TypeError: editProfile() got an unexpected keyword argument 'obj' 类型错误:启动()有一个意外的关键字参数“超时” - TypeError: initiate() got an unexpected keyword argument 'timeout'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM