Skip to content

How to custom for list view and add method DELETE in ListCreateAPIView #8507

Discussion options

You must be logged in to vote

Hi! Let's start with the filtering, because you can also use that on DELETE!

If you want to add basic filtering via query parameters, you can override the get_queryset() to manually add this logic:

class TutorialList(generics.ListCreateAPIView):
    queryset = Tutorial.objects.all()
    serializer_class = TutorialSerializer
    filter_params = ["title", "author"]
    
    def get_queryset(self):
        queryset = super().get_queryset()
        filter_kwargs = dict()
        for param in self.filter_params:
            value = self.request.query_params.get(param)
            if value is not None:
                filter_kwargs[param] = value
        queryset = queryset.filter(**filter_kwargs

Replies: 1 comment 1 reply

This comment has been minimized.

@kev26
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants