Kevin Browns answer is fantastic, however may be slightly out of date now.
Running the AllDjangoFilterBackend
filter backend with django-filter==2.1.0 throws the following:
Setting 'Meta.model' without either 'Meta.fields' or 'Meta.exclude' has been deprecated since 0.15.0 and is now disallowed. Add an explicit 'Meta.fields' or 'Meta.exclude' to the AutoFilterSet class.
It seems that simply replacing fields = None
with exclude = ''
is sufficient to use all fields. Full code below:
from django_filters.rest_framework import DjangoFilterBackend
class AllDjangoFilterBackend(DjangoFilterBackend):
'''
Filters DRF views by any of the objects properties.
'''
def get_filter_class(self, view, queryset=None):
'''
Return the django-filters `FilterSet` used to filter the queryset.
'''
filter_class = getattr(view, 'filter_class', None)
filter_fields = getattr(view, 'filter_fields', None)
if filter_class or filter_fields:
return super().get_filter_class(self, view, queryset)
class AutoFilterSet(self.default_filter_set):
class Meta:
exclude = ''
model = queryset.model
return AutoFilterSet
Save this to your_project/your_app/filters.py (or similar) and then ensure your settings file contains:
REST_FRAMEWORK = {
'DEFAULT_FILTER_BACKENDS': (
'your_project.your_app.filters.AllDjangoFilterBackend'
),
}