Jai Simha Ramanujapura
Jai Simha Ramanujapura

Reputation: 340

Django rest framework filters and search not working when def list() is added to model viewsets

I am working on API written in Django rest framework. I have added Search filters and ordering filters in my ModelViewSet and were working fine.

class bookviewset(ModelViewSet):

    queryset = Book.objects.all()
    serializer_class = book_serializer
    filter_class = bookfilter
    filter_backends = ( django_filters.rest_framework.DjangoFilterBackend,filters.OrderingFilter,filters.SearchFilter)
    ordering_fields = ('created_at', 'id','price_ids__price',)
    search_fields = ('name', 'description', 'tag_ids__tag_name', 'category_ids__category')

But, When I override def list(self, request, *args, **kwargs): inside Modelviewset, all the filters have stopped working.

Is there a way to enable all the filters again?

Thank you.

Upvotes: 4

Views: 2167

Answers (1)

Dima  Kudosh
Dima Kudosh

Reputation: 7376

You must manually filter your queryset using this filter_queryset method. So add this line to your list method.

def list(self, request, *args, **kwargs):
    # fetch qs
    qs = self.filter_queryset(qs)
    # etc

Upvotes: 3

Related Questions