'DRF update username api

I can't figure out why the line below is not working

self.object.username = serializer.data.get("new_username")

Every time I'm sending PUT request, it returns changed: "Success", but doesn't change username in database

views.py

class ChangeUsernameView(APIView):
    permissions_classes = [IsAuthenticated]

    def get_object(self):
        return self.request.user

    def put(self, request):
        self.object = self.get_object()
        serializer = ChangeUsernameSerializer(data=request.data)

        if serializer.is_valid():
            curr_password = serializer.data.get("curr_password")
            if not self.object.check_password(curr_password):
                return Response({"curr_password": ["Wrong password."]},
                                status=status.HTTP_400_BAD_REQUEST)
            self.object.username = serializer.data.get("new_username")
            self.object.save()
            return Response({"changed": "Success"}, status=status.HTTP_200_OK)

serializer

class ChangeUsernameSerializer(serializers.Serializer):

    new_username = serializers.CharField(required=True)
    curr_password = serializers.CharField(required=True)


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source