[Django]-How to Perform a Delete operation in Django rest Framework?

0👍

✅

There’s also a generic delete class based view as shown here

8👍

ModelViewSet has a function which is called when you do DELETE request and it is called destroy

This is how it looks like:

    def destroy(self, request, *args, **kwargs):
        instance = self.get_object()
        self.perform_destroy(instance)
        return Response(status=status.HTTP_204_NO_CONTENT)

So to delete the instance from your DB – you can do

instance.delete()

that’s what actually perform_destroy does.

So you can take inspiration from this and implement your custom delete.

Leave a comment