How to override list function of ModeViewSet to filter multiple value of same parameters in Django?

Currently this how my code looks -

class MyModelViewSet(viewsets.ModelViewSet):
    queryset = MyModel.objects.all()
    serializer_class = MyModelSerializer

    def list(self, request):
        query_dict = QueryDict(request.META['QUERY_STRING'])
        query_dict = query_dict.dict()
        self.queryset = MyModel.objects.filter(**query_dict)
        return super().list(reuqest)

Now if I invoke the endpoint with the api like /api/url/?param1=79&param2=34 it works fine. Assuming param1 and param2 are two fields present in MyModel. But if try to do this - /api/url/?param1=79&param2=34&param1=45&param2=576 it returns the result of param1=45 AND param2=576. Which is understandable since I am using dict to parse query params.

But I want the results of param1=79&param2=34&param1=45&param2=576 combination. How to achieve that?

Have you tried this?

params1 = request.query_params.getlist('param1', '')
Back to Top