Django REST Framework: Validate before a delete

You can solve it with permissions:

from rest_framework import permissions

class IsSurvey(permissions.BasePermission):


    def has_object_permission(self, request, view, obj):

        if request.method == 'DELETE' and obj.survey:
            return False


        return True

Then add to your view's permission_classes

from permissions import IsSurvey

class MyViewSet(viewsets.ModelViewSet):
    permission_classes = (IsSurvey, )

The solution I found was to override the destroy method on the api.

class CallDetail(generics.RetrieveUpdateDestroyAPIView):
    queryset = XCall.objects.all()
    serializer_class = CallSerializer
    ...

    def destroy(self, request, *args, **kwargs):
        obj = self.get_object()
        if obj.survey:
            return Response(data={'message': "Too late to delete"},
                            status=status.HTTP_400_BAD_REQUEST)
        self.perform_destroy(obj)
        return Response(status=status.HTTP_204_NO_CONTENT)

For me makes much more sense to validate on the destroy method instead of validating on the object permission check as avances123 mentioned, since permission should only check for permission stuffs, and doesn't return any messages related to validation.

Hope that helps ;)