使用Django Rest Framework使用自定义序列化程序CharField对该函数获取无效的关键字参数

Dav*_*win 2 django angularjs django-rest-framework

我在这里基于Thinkster IO教程为应用程序编写自定义用户身份验证:https ://thinkster.io/django-angularjs-tutorial/

我已经对用户创建进行了调整,以使用确认的密码,然后进行保存。

我以为我一切都很好,但是我陷入了一个错误。

我相信这是问题代码。

def create(self, validated_data):
    # get the password or leave it alone
    password = validated_data.get('password', None)
    # get the confirm_password or leave it alone
    confirm_password = validated_data.get('confirm_password', None)

    # If the password exists AND the confirm password exists AND they eaqual each other
    if password and confirm_password and password == confirm_password:
        # create the object with valid form data
        return models.Account.objects.create(**validated_data)
Run Code Online (Sandbox Code Playgroud)

错误是: 'confirm_password' is an invalid keyword argument for this function

看起来好像是由我对API的看法触发的。为什么会这样呢?在我看来,这不应该成为问题,因为保存之前在视图中进行了检查,并且如果匹配则仅使用密码。

任何帮助都会很棒。

这是完整的序列化器

from django.contrib.auth import update_session_auth_hash

from rest_framework import serializers

from . . import models


class AccountSerializer(serializers.ModelSerializer):
    password = serializers.CharField(write_only=True, required=False)
    confirm_password = serializers.CharField(write_only=True, required=False)

    class Meta:
        model = models.Account
        fields = ('id', 'email', 'username', 'created_at', 'updated_at',
                  'first_name', 'last_name', 'password',
                  'confirm_password',)
        read_only_fields = ('created_at', 'updated_at',)

        def create(self, validated_data):
            # get the password or leave it alone
            password = validated_data.get('password', None)
            # get the confirm_password or leave it alone
            confirm_password = validated_data.get('confirm_password', None)

            # If the password exists AND the confirm password exists AND they eaqual each other
            if password and confirm_password and password == confirm_password:
                # create the object with valid form data
                return models.Account.objects.create(**validated_data)

        def update(self, instance, validated_data):
            # get the username, if not changed that use the instance username
            instance.username = validated_data.get('username', instance.username)
            # update the instance
            instance.save()
            # get the password or leave it alone
            password = validated_data.get('password', None)
            # get the confirm_password or leave it alone
            confirm_password = validated_data.get('confirm_password', None)

            # If the password exists AND the confirm password exists AND they eaqual each other
            if password and confirm_password and password == confirm_password:
                # create the password with the password submitted
                instance.set_password(password)
                #save that instance with the updated password
                instance.save()

            # update the session so that the user does not need to login again
            update_session_auth_hash(self.context.get('request'), instance)

            # return the changed (or not changed) record
            return instance
Run Code Online (Sandbox Code Playgroud)

小智 5

您在帐户模型中保存数据的方式似乎不正确。

首先,confirm_password您的帐户模型中是否有一个字段?(我相信你没有,这就是错误的意思)

解:

# use .pop() instead of .get() so that it will be removed from the dict
# `None` to avoid KeyError. so pop the value if it exists
confirm_password = validated_data.pop('confirm_password', None)
...
if password and confirm_password and password == confirm_password:
    account = Account.objects.create(**validated_data)
    # I assume you extend the User model to this model. you need
    # to use .set_password() so that the password won't be saved as raw
    account.set_password(password)
    account.save()

    return account
Run Code Online (Sandbox Code Playgroud)