Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs 使用Django Rest Framework获取具有自定义序列化程序CharField的此函数的无效关键字参数_Angularjs_Django_Django Rest Framework - Fatal编程技术网

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

Angularjs 使用Django Rest Framework获取具有自定义序列化程序CharField的此函数的无效关键字参数,angularjs,django,django-rest-framework,Angularjs,Django,Django Rest Framework,我正在根据Thinkster IO教程为应用程序编写自定义用户身份验证: 我已经调整了用户创建,使用确认密码,然后进行保存 我原以为一切都很顺利,但我有一个错误 我相信这就是问题代码 def create(self, validated_data): # get the password or leave it alone password = validated_data.get('password', None) # get the confirm_password

我正在根据Thinkster IO教程为应用程序编写自定义用户身份验证:

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

我原以为一切都很顺利,但我有一个错误

我相信这就是问题代码

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)
错误是:
“confirm\u password”是此函数的无效关键字参数

它看起来像是由我的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

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

首先,您的帐户模型中是否有“确认密码”字段?(我相信你没有,这就是错误的含义)

解决方案:

# 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

我不太明白。这在序列化程序中,并且正在使用声明的序列化程序字段。因此,不,它不存在于模型中。请修改create()函数并在我的回答中添加代码进行了更改,但我仍然得到
“confirm\u password”是此函数的无效关键字参数
错误