[Django]-How to return custom JSON response in UpdateAPIView?

4👍

you can override update from UpdateAPIView like this:

class DetailUpdateView(generics.UpdateAPIView):
    permission_classes = (permissions.IsAuthenticated,)
    lookup_field = "pk"
    serializer_class = DetailSerializers

    def get_queryset(self):
       data = Data.objects.all()
       return data

    def update(self, request, *args, **kwargs):
       partial = kwargs.pop('partial', False)
       instance = self.get_object()
       serializer = self.get_serializer(instance, data=request.data, partial=partial)
       serializer.is_valid(raise_exception=True)
       self.perform_update(serializer)
       result = {
        "message": "success",
        "details": serializer.data,
        "status": 200,

       }
       return Response(result)

Leave a comment