Django REST - SearchFilter not filtering

Oz Bar-Shalom picture Oz Bar-Shalom · Dec 17, 2016 · Viewed 8.8k times · Source

I am using Django 1.10 and django-rest-framework 3.5.3. I would like to have a generic search query: search for a value on many fields. I Found the SearchFilter on the Docs.

I tried to add the filter backend to the ViewSet, but it seems not to be working. Any search query response with all the objects.

ViewSet:

from rest_framework import filters

class UserViewSet(viewsets.ModelViewSet):
    queryset = User.objects.all().order_by('-date_joined')
    serializer_class = UserSerializer
    filter_backends = (filters.SearchFilter,)
    filter_fields = ['username', 'email', 'is_staff', 'groups']

Query url:

http://localhost:8000/users/?search=something

In addition, at the docs they show that a new button "Filter" added to the django rest web page. It does not in my case.

Answer

serg picture serg · Dec 18, 2016

Instead of filter_fields use search_fields. filter_fields is for enabling filtering on specific fields, like /users/?username=something&is_staff=True.