3 回答

TA貢獻(xiàn)1877條經(jīng)驗(yàn) 獲得超6個(gè)贊
我不完全確定,但似乎在serializers.py 文件中,您正在創(chuàng)建一個(gè)從該類繼承的類serializers.Serializer
。您的代碼顯示fields = ("password", "token", "uidb64",)
,這似乎與父serializers.Serializer
類中的字段發(fā)生沖突。因此,一種解決方法是將該field
變量重命名為類似my_field
.
如果您打算覆蓋該變量,那么它應(yīng)該是一個(gè)列表,而不是一個(gè)元組。將其從 更改fields = ("password", "token", "uidb64",)
為fields = ["password", "token", "uidb64"]

TA貢獻(xiàn)1790條經(jīng)驗(yàn) 獲得超9個(gè)贊
您的序列化器需要Meta聲明模型字段。
嘗試這個(gè):
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貢獻(xiàn)1796條經(jīng)驗(yàn) 獲得超7個(gè)贊
應(yīng)該fields在Meta類內(nèi)
class SetNewPasswordSerializer(serializers.Serializer):
# rest of your code
class Meta:
fields = ("password", "token", "uidb64",)
添加回答
舉報(bào)