angularjs - 使用 Django Rest Framework 通过自定义序列化器 CharField 获取此函数的无效关键字参数

标签 angularjs django 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)

错误是:'confirm_password' 是此函数的无效关键字参数

看起来它是由我的 API View 触发的。对于为什么会发生这种情况有什么想法吗?在我看来,这不应该是一个问题,因为检查是在保存之前的 View 中,并且如果匹配,它只会使用密码。

任何帮助都会很棒。

这是完整的序列化器

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

最佳答案

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

首先,您的帐户模型中是否有 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

关于angularjs - 使用 Django Rest Framework 通过自定义序列化器 CharField 获取此函数的无效关键字参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32060645/

相关文章:

python - 使用未跟踪的 .env 文件部署到 Heroku

python - Django REST - 无法访问我的 API 的编辑页面

javascript - Ajax 使用 Django Rest Framework 收到 403 禁止错误

python - 如何处理在 javascript 中生成并在 python DRF 中使用的请求主体 JSON 字段的命名约定差异?

angularjs 路由单元测试

javascript - 如何在 Angularjs 中创建过滤器?

html - <a> inside <li> 的 border-bottom 和 hover 的样式显示

javascript - Angular 绑定(bind)表到通用对象

python - django 管道根本不工作

django - 在安装 Django-nonrel 之前是否需要卸载 Django 1.3?