I noticed that list, retrieve and update all go through the serializer to perform the actions but destroy does not. My guess is because in these cases a response of the object(s) is required. What I really want to do is perform some validations before deleting an object.
I found this Stackoverflow post which touches the subject but the answers felt like a workarounds to me.
Currently I have 2 ways to do that,
- Validate in pre_delete() as suggested in SO.
- I feel pre_delete() is already too late to make a decision to delete or not.
- Even though this works for now, I feel signals were not built for this purpose based on the following definition from Django docs.
- Validation in perform_delete() in the View.
- Again doesn't feel rite considering Serializers have a very nice way to validate objects.
Django includes a “signal dispatcher” which helps allow decoupled applications get notified when actions occur elsewhere in the framework. In a nutshell, signals allow certain senders to notify a set of receivers that some action has taken place. They’re especially useful when many pieces of code may be interested in the same events.
So I was curious why delete is missing in ModelSerializers? Are there any reasons that this is not being done? If so, then apart from the above ways to validate objects before deleting them are there other approaches I can try?
Thanks,
Vignesh
P.S: I just started using restframework and I love it so far! 😁