OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Append an entry per view to the DEFAULT_FILTER_BACKENDS

  • Thread starter Thread starter cezar
  • Start date Start date
C

cezar

Guest
In a Django (and DRF) project with a large number of views we have set a list of filter backends in settings.py:

Code:
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:

Code:
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?
<p>In a Django (and DRF) project with a large number of views we have set a list of filter backends in <code>settings.py</code>:</p>
<pre><code>REST_FRAMEWORK = {
"DEFAULT_FILTER_BACKENDS": [
# filter backend classes
],
# other settings
}
</code></pre>
<p>Some of the view classes need additional filter backends. We can specify the attribute <code>filter_backends</code> per view class:</p>
<pre><code>FooViewSet(viewsets.ModelViewSet):
filter_backends = [DefaultFilterOne, DefaultFilterTwo, DefaultFilterThree, AdditionalFilter]
</code></pre>
<p>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 <code>settings.py</code> it has to be reflected in the view class.</p>
<p>What is best practice to append an additional filter backend per class, while keeping the default filter backends from <code>settings.py</code>?</p>
 
Top