Django Rest Framework does not filter after resetting queryset

Asked

Viewed 21 times

0

My url will be something like.../? search=modal.

I want to replace the "modal" with empty "" so that clean the filter and return all records.

Views.py

class AnexoExamesViewSet(viewsets.ModelViewSet):

search_fields = ['descr']
filter_backends = (filters.SearchFilter,)
queryset = AnexoExames.objects.all()
serializer_class = AnexoExamesSerializer

def get_queryset(self):
    queryset = AnexoExames.objects.all()
    search_descr = self.request.query_params.get('search',None)
    print(search_descr)
    if search_descr=='modal':            
        queryset = AnexoExames.objects.filter(descr='')                
    return queryset

This way it is returning zero results

1 answer

0


def get_queryset(self):
    qs = self.queryset
    search_descr = self.request.query_params.get('search', None)

    if search_descr=='modal':            
        qs = qs.filter(descr='')                
    return qs

Browser other questions tagged

You are not signed in. Login or sign up in order to post.