属性错误:“元组”对象没有属性“值”

我有一个基本的 API 来重置密码,但是,尽管有“值”,但它似乎抛出了此错误,并没有完全出现在我的代码中:


views.py


class PasswordResetNewPasswordAPIView(GenericAPIView):

    serializer_class = SetNewPasswordSerializer


    def patch(self, request):

        user = request.data

        serializer = SetNewPasswordSerializer(data=user)

        serializer.is_valid(raise_exception=True)

        return Response({

            "message": "password reset"},

            status=status.HTTP_200_OK

        )

serializers.py


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)


    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)

urls.py


...

path('password-reset-setup/', PasswordResetNewPasswordAPIView.as_view(),name="password-reset-setup"),


可能的错误是什么?我该如何解决它?


qq_花开花谢_0
浏览 112回答 3
3回答

慕哥9229398

我不完全确定,但似乎在serializers.py 文件中,您正在创建一个从该类继承的类serializers.Serializer。您的代码显示fields = ("password", "token", "uidb64",),这似乎与父serializers.Serializer类中的字段发生冲突。因此,一种解决方法是将该field变量重命名为类似my_field.如果您打算覆盖该变量,那么它应该是一个列表,而不是一个元组。将其从 更改fields = ("password", "token", "uidb64",)为fields = ["password", "token", "uidb64"]

富国沪深

您的序列化器需要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)

芜湖不芜

应该fields在Meta类内class SetNewPasswordSerializer(serializers.Serializer):    # rest of your code    class Meta:        fields = ("password", "token", "uidb64",)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python