carltongibson / django-filter

A generic system for filtering Django QuerySets based on user selections

Home Page:https://django-filter.readthedocs.io/en/main/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Please add "get_filterset_class" method support for drf views

tvboxme opened this issue · comments

As the title.

When I want to use more than one filterset_class in one View, just like using "get_serializer_class" method to return different serializer_class for each action. I found django-filter only support attribute "filterset_class", but not method "get_filterset_class".

I do think that is great, does anyone think so?

Since you mention DRF I'll assume you're using that. See https://django-filter.readthedocs.io/en/stable/guide/rest_framework.html#overriding-filterset-creation — the DjangoFilterBackend already defines the .get_filterset_class() method.

@carltongibson I believe the request was to support the definition of a get_filterset_class method in the view where the filter is used as an alternative of setting the filterset_class directly. This is similar to DRF for serializer_class and get_serializer_class. I had the same issue and I solved it by defining filterset_class as a property directly in the view:

@property
def filterset_class(self):
    if self.action == 'no-filter':
        return None
    return MyFilter

It does not follow the DRF pattern but it works.