[Django]-Why is my Django Rest Framework Search filter not working?

7👍

This basically doesn’t work because you’re trying to do too much. You’ve written your own get method which bypasses all the magic of the DRF views. In particular, by not calling GenericAPIView.get_object, you avoid a line that looks like

    queryset = self.filter_queryset(self.get_queryset())

which is where the QuerySet is filtered. This simpler version, practically identical to the one in the SearchFilter docs, should work

from rest_framework import status, filters, generics

class JobView(generics.LisaAPIView):
    queryset = Job.manager.all()
    serializer_class = JobSerializer
    filter_backends = [filters.SearchFilter]
    search_fields = ['name']

NOTE based on your question, I am assuming:

  1. that your Job model has a name field

  2. that for some reason you’ve renamed the Job manager to manager via a call to models.Manager()

👤RishiG

0👍

I think you should filter your queryset based on the parameter you’re sending via GET, because it wont happen automatically. use request.query_params.get('search') to access your parameter.

Leave a comment