Chray
Chray

Reputation: 21

Triggering a django rest framework update() function using HTTP Requests

I just started to learn django last week so please excuse my ignorance if I'm completely approaching this problem the wrong way.

So I've been following a thinkster tutorial on setting up a User model that allows the change of a password in the model. So far I have a url (/api/user) that leads to this view:

class UserRetrieveUpdateAPIView(RetrieveUpdateAPIView):
    permission_classes = (IsAuthenticated,)
    renderer_classes = (UserJSONRenderer,)
    serializer_class = UserSerializer

    def retrieve(self, request, *args, **kwargs):
        #turns the object recieved into a JSON object
        serializer = self.serializer_class(request.user)
        return Response(serializer.data, status=status.HTTP_200_OK)

    def update(self, request, *args, **kwargs):
        serializer_data = request.data
        serializer = self.serializer_class(
            request.user, data=serializer_data, partial=True
        )
        serializer.is_valid(raise_exception=True)
        serializer.save()

        return Response(serializer.data, status=status.HTTP_200_OK)

I understand that this section :

serializer = self.serializer_class(
        request.user, data=serializer_data, partial=True
    )
    serializer.is_valid(raise_exception=True)
    serializer.save()

will call upon a serializer class along the lines of:

class UserSerializer(serializers.ModelSerializer):
    #This class handles serialization and deserialization of User objects
    password = serializers.CharField(
        max_length=128,
        min_length=8,
        write_only=True
    )
    class Meta:
        model = User
        fields = ('email', 'username', 'password', 'token',)
        read_only_fields=('token',)

    def update(self, instance, validated_data):
        #performs an update on the user
        password = validated_data.pop('password', None) 
        #have to take out password because setattr does not handle hashing etc

        for (key, value) in validated_data.items():
            #for the keys after taking out password set them to the updating User instance
            setattr(instance, key, value)

        if password is not None:
            instance.set_password(password) #set_password is handled bydjango 

        instance.save() #set_password does not save instance
        return instance

again I understand this section will essentially take request.data and "update" the model. However I'm stuck on how to test this feature using Postman.

Currently when I send a GET request to the URL using Postman I get this response:

GET Request result

The response is based off of my authenticate class that uses JWT authentication. My question is, how do I trigger that update function using a Postman HTTP Request.

Upvotes: 2

Views: 1454

Answers (1)

Ykh
Ykh

Reputation: 7717

PATCH(partial_update) or PUT(update) http://127.0.0.1:8000/api/user/user_id/

you can see router table here

Upvotes: 2

Related Questions