4👍
You just need to return the data. You have kept the return statement inside if
condition. Just take it outside of if condition.
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
And change your view as follows
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)
Source:stackexchange.com