Why does ModelSerializer have no way to delete the instance?

2,423 views
Skip to first unread message

Vignesh

unread,
Sep 14, 2017, 2:00:57 AM9/14/17
to Django REST framework
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,
  1. Validate in pre_delete() as suggested in SO.
    1. I feel pre_delete() is already too late to make a decision to delete or not.
    2. Even though this works for now, I feel signals were not built for this purpose based on the following definition from Django docs. 
  2. Validation in perform_delete() in the View.
    1. 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! 😁

Xavier Ordoquy

unread,
Sep 14, 2017, 5:25:35 AM9/14/17
to django-res...@googlegroups.com
Deletion usually doesn’t change model’s field and doesn’t require data from the client.
Therefore there’s no real reason a serializer is required, in particular since the requisites are on the instance.
Overriding `perform_delete` is definitively what I would do and return a 400 with a error message.

Regards,
Xavier Ordoquy,
Linovia.

--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-fram...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Vignesh

unread,
Sep 16, 2017, 1:31:34 AM9/16/17
to Django REST framework
Alright, thats what I've been doing so far. Thanks!
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-framework+unsub...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages