Delete multiple entries with URL filter


I’ve just started learning to create Django Rest APIs. I have a model containing data, and I’m using the django-filters library to add URL filtering. My test views are implemented as:

class SiteList(generics.ListCreateAPIView):
    queryset = Site.objects.all()
    serializer_class = SiteSerializer

    filter_backends = [DjangoFilterBackend]
    filterset_fields = ['client',]

class SiteDetail(generics.RetrieveUpdateDestroyAPIView):
    queryset = Site.objects.all()
    serializer_class = SiteSerializer

I want to be able send a DELETE on that would result in all sites with a client id of 2 being deleted. The filterset_fields will expand in the future, trying to keep everything as simple as possible.

I understand ListCreateAPIView doesn’t allow DELETE requests but I have no idea how to go about changing things to work with the django-filters library. I know I could send delete requests from the front-end (react) individually for each item I want deleted but this feels really inefficient.


Actually, according to the information at ListCreateAPIView -- Classy DRF, delete is one of the valid method names.

It would appear to me that you should be able to add a delete method to your SiteList class to process that request, using whatever information is supplied in that request.

It might be something as simple as:

def delete(self, request, *args, **kwargs):
    number_deleted = self.filtered_queryset(request).delete()

And then you still need to determine where you go from there…

(Just a guess - I have no way to test / verify this at the moment.)