[Fixed]-Override serializer delete method in Django RF


If you’re using a ModelViewSet, you could do it in the view:

class YourViewSetClass(ModelViewSet):

    def destroy(self, request, *args, **kwargs):
       user = request.user # deleting user
       # you custom logic # 
       return super(YourViewSetClass, self).destroy(request, *args, **kwargs)

The destroy method is so simple (just a call to instance.delete()) that the action is not delegated to the serializer. The serializers in DRF are for negotiating external representations to/from your database models. Here you simply want to delete a model.



I think you can do that but in the view level.

So if you’re using ModelViewsets you can override the destory method or the perform_destroy and add your business logic.


Leave a comment