我正在根据此处的 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/