2
votes

I can't get the browsable API to show the "Filters" button after configuring django to use the filtering backend.

According to the documentation all I need to do is add the following few lines of code into the site's settings.py file and the filters should automatically be in the browsable API's web interface, and I just don't see it there. I tried restarting the web server (I'm working with ./manage runserver) and that didn't help.

EDIT:

I know there's another option to turn the filters on a view-basis, but I want to have them on for all views.

According to documentation, it should be enough to do only one of those:

The default filter backends may be set globally, using the DEFAULT_FILTER_BACKENDS setting.

or following:

You can also set the filter backends on a per-view, or per-viewset basis

From settings.py:

REST_FRAMEWORK = {

  <snip>

  'DEFAULT_FILTER_BACKENDS': (
    'django_filters.rest_framework.DjangoFilterBackend',
  ),

Following content of pip freeze:

(venv) mba15:server nir$  pip freeze | grep django
django-allauth==0.28.0
django-celery==3.1.17
django-filter==0.15.3
django-registration-redux==1.4
django-rest-auth==0.8.2
djangorestframework==3.5.3
2

2 Answers

2
votes

Have you either specified filter_fields on your ViewSet or set a filter_class?

class ArticleViewSet(ModelViewSet):
    serializer_class = ArticleSerializer
    queryset = Article.objects.all()
    filter_fields = ('category', )

or

class ArticleFilterSet(FilterSet):
    class Meta:
        model = Article
        fields = ('category', )


class ArticleViewSet(ModelViewSet):
    serializer_class = ArticleSerializer
    queryset = Article.objects.all()
    filter_class = ArticleFilterSet
1
votes

I have had a similar issue - the Filers button was missing from Browsable API view.

fields (in MyFilter.Meta), filter_class, filter_backends was all set. Strange about it was the Filers button was present on other views.

class MyViewSet(viewsets.ViewSetMixin, generics.ListAPIView):
    filter_class = MyFilter
    filter_backends = (OrderingFilter, DjangoFilterBackend)
    ...

    def list(self, request):
        data = {'foo': 'bar'}
        return Response(data)

The problem went away when I've put 'results' to the returned JSON, e.i. data = {'foo': 'bar', 'results': None}. No idea why that worked.