One method may be to override the restore_object method in the Serializer. It will look something like this:
class UserSerializer(serializers.ModelSerializer): class Meta: model = User fields = ('id', "username", 'email', 'first_name', 'last_name', 'password')
Now that this is deserialized into an instance of the object, you will have a valid hashed password. Then you can accomplish what you want by changing the view that you currently have a little.
class UserChangePassword(APIView): def patch(self, request): serialized = UserSerializer(data=request.DATA) if serialized.is_valid(): serialized.save() return Response(status=status.HTTP_205_RESET_CONTENT) else: return Response(serialized.errors, status=status.HTTP_400_BAD_REQUEST)
And I believe that the JSON in your PATCH request (depending on the type of search, which in my opinion is id by default) looks something like this:
{ "id": "83", "password": "12345" }
Hope this helps!
EDIT:
Note that, as Symmetric points out in the comments, restore_object deprecated in DRF 3.0
jnishiyama
source share