Django REST Framework: Validate before a delete

django, django-rest-framework, validation

Solution

Rather than raising a `ValidationError`, I will just raise a ParseError or another custom error that fits the error description:

from rest_framework import exceptions
def pre_delete(self, obj):
        if obj.survey:
            raise exceptions.ParseError("Too late to delete")

Problem

I want to run a validation before an object is deleted, to prevent deletion in certain cases and return as a validation error. How do I do that? What I have currently doesn't seem right: ``` class CallDetail(generics.RetrieveUpdateDestroyAPIView): queryset = XCall.objects.all() serializer_class = CallSerializer ... def pre_delete(self, obj): if obj.survey: raise serializers.ValidationError("Too late to delete") ```

Original source