3 回答
TA贡献1877条经验 获得超6个赞
我不完全确定,但似乎在serializers.py 文件中,您正在创建一个从该类继承的类serializers.Serializer
。您的代码显示fields = ("password", "token", "uidb64",)
,这似乎与父serializers.Serializer
类中的字段发生冲突。因此,一种解决方法是将该field
变量重命名为类似my_field
.
如果您打算覆盖该变量,那么它应该是一个列表,而不是一个元组。将其从 更改fields = ("password", "token", "uidb64",)
为fields = ["password", "token", "uidb64"]
TA贡献1790条经验 获得超9个赞
您的序列化器需要Meta声明模型字段。
尝试这个:
class SetNewPasswordSerializer(serializers.Serializer):
password = serializers.CharField(max_length=50, write_only =True)
token = serializers.CharField(write_only =True)
uidb64 = serializers.CharField(max_length = 255, write_only =True)
class Meta: # you missed meta to declare your model fields
fields = ("password", "token", "uidb64",)
def validate(self, attrs):
try:
password = attrs.get("password", "")
token = attrs.get("token", "")
uidb64 = attrs.get("uidb64", "")
print(uidb64)
id = force_str(urlsafe_base64_decode(uidb64))
print(id)
user = Author.objects.get(id=id)
if not PasswordResetTokenGenerator().check_token(user, token):
raise AuthenticationFailed("Invalid Reset Parameter", 401)
user.set_password(password)
user.save()
return user
except Exception:
raise AuthenticationFailed("Invalid Reset Parameter", 401)
return super().validate(attrs)
TA贡献1796条经验 获得超7个赞
应该fields在Meta类内
class SetNewPasswordSerializer(serializers.Serializer):
# rest of your code
class Meta:
fields = ("password", "token", "uidb64",)
添加回答
举报