How to make a PATCH request using DJANGO REST framework

class DetailView(APIView):
    def get_object(self, pk):
        return TestModel.objects.get(pk=pk)

    def patch(self, request, pk):
        testmodel_object = self.get_object(pk)
        serializer = TestModelSerializer(testmodel_object,, partial=True) # set partial=True to update a data partially
        if serializer.is_valid():
            return JsonResponse(code=201,
        return JsonResponse(code=400, data="wrong parameters")

You do not need to write the partial_update or overwrite the update method. Just use the patch method.

Leave a Comment