gpt4 book ai didi

python - AssertionError : . validate() 应该返回经过验证的数据

转载 作者:行者123 更新时间:2023-12-04 15:23:05 26 4
gpt4 key购买 nike

我能够访问 PasswordChangeSerializer 的 validate() 函数的 user_queryset,但是我仍然收到此错误:

assert value is not None, '.validate() should return the validated data'.

序列化器.py


class PasswordChangeSerializer(serializers.ModelSerializer):
old_password = serializers.CharField(write_only=True, required=True, max_length=30)
new_password = serializers.CharField(write_only=True, required=True, max_length=30)
confirm_password = serializers.CharField(write_only=True, required=True, max_length=30)

class Meta:
model = User
fields = ['old_password', 'new_password', 'confirm_password']


def validate(self, data):
old_password = data['old_password']
new_password = data['new_password']
confirm_password = data['confirm_password']

user_queryset = User.objects.all()

if user_queryset.exists() and user_queryset.count() == 1:
user_set = user_queryset.first()

if not user_set.check_password(old_password):
raise serializers.ValidationError({'old_password': 'Wrong password!'})

if confirm_password != new_password:
raise serializers.ValidationError({'password': 'Password must be confirmed correctly!'})

return data

def update(self, instance, validated_data):
instance.set_password(validated_data['new_password'])
instance.save()
return instance

View .py


class PasswordChangeAPIView(APIView):
permission_classes = [IsAdminUser]
serializer_class = PasswordChangeSerializer

def post(self, request, format=None):
data = request.data
password_ser = PasswordChangeSerializer(data=data)
if password_ser.is_valid():
password_ser.save()
new_data = password_ser.data
return Response(new_data, status=HTTP_201_CREATED)
else:
return Response({"msg":"invalid user!"}, status=HTTP_400_BAD_REQUEST)

最佳答案

你只需要返回数据。您已将 return 语句保留在 if 条件中。把它放在 if 条件之外。

def validate(self, data):
old_password = data['old_password']
new_password = data['new_password']
confirm_password = data['confirm_password']

user_queryset = User.objects.all()

if user_queryset.exists() and user_queryset.count() == 1:
self.user_set = user_queryset.first()

if not self.user_set.check_password(old_password):
raise serializers.ValidationError({'old_password': 'Wrong password!'})

if confirm_password != new_password:
raise serializers.ValidationError({'password': 'Password must be confirmed correctly!'})

return data

然后改变你的观点如下

class PasswordChangeAPIView(APIView):
permission_classes = [IsAdminUser]
serializer_class = PasswordChangeSerializer

def post(self, request, format=None):
data = request.data
password_ser = PasswordChangeSerializer(data=data)
if password_ser.is_valid():
user = password_ser.user_set
user.set_password(data['new_password'])
user.save()
return Response(new_data, status=HTTP_201_CREATED)
else:
return Response({"msg":"invalid user!"}, status=HTTP_400_BAD_REQUEST)

关于python - AssertionError : . validate() 应该返回经过验证的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62890249/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com