In a Django (and DRF) project with a large number of views we have set a list of filter backends in settings.py
:
REST_FRAMEWORK = {
"DEFAULT_FILTER_BACKENDS": [
# filter backend classes
],
# other settings
}
Some of the view classes need additional filter backends. We can specify the attribute filter_backends
per view class:
FooViewSet(viewsets.ModelViewSet):
filter_backends = [DefaultFilterOne, DefaultFilterTwo, DefaultFilterThree, AdditionalFilter]
However, this is not DRY. Here in this example three (3) filter backends are default and have to be repeated in the viewset class. If there’s a change in settings.py
it has to be reflected in the view class.
What is best practice to append an additional filter backend per class, while keeping the default filter backends from settings.py
?