NirIzr NirIzr - 1 month ago 21
Python Question

Django rest framework browsable API filters backend not showing

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.


and 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

Answer

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