How to make a PATCH request using DJANGO REST framework

前端 未结 6 1904
北恋
北恋 2020-12-13 13:11

I am not very experience with Django REST framework and have been trying out many things but can not make my PATCH request work.

I have a Model serializer. This is

6条回答
  •  無奈伤痛
    2020-12-13 13:16

    I ran into this issues as well, I solved it redefining the get_serializer_method and adding custom logic to handle the partial update. Python 3

     class ViewSet(viewsets.ModelViewSet):
         def get_serializer_class(self):
             if self.action == "partial_update":
                 return PartialUpdateSerializer
    

    Note: you may have to override the partial_update function on the serializer. Like so:

    class PartialUpdateSerializer(serializers.Serializer):
        def partial_update(self, instance, validated_data):
           *custom logic*
           return super().update(instance, validated_data)
    

提交回复
热议问题