Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: allow passing a filterset class to the filterset factory #1636

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions django_filters/filterset.py
Original file line number Diff line number Diff line change
Expand Up @@ -458,9 +458,15 @@ class FilterSet(BaseFilterSet, metaclass=FilterSetMetaclass):
pass


def filterset_factory(model, fields=ALL_FIELDS):
meta = type(str("Meta"), (object,), {"model": model, "fields": fields})
filterset = type(
str("%sFilterSet" % model._meta.object_name), (FilterSet,), {"Meta": meta}
def filterset_factory(model, filterset=FilterSet, fields=None):
attrs = {"model": model}
if fields is None:
if getattr(getattr(filterset, "Meta", {}), "fields", None) is None:
attrs["fields"] = ALL_FIELDS
else:
attrs["fields"] = fields
bases = (filterset.Meta,) if hasattr(filterset, "Meta") else ()
Meta = type("Meta", bases, attrs)
return type(filterset)(
str("%sFilterSet" % model._meta.object_name), (filterset,), {"Meta": Meta}
)
return filterset
19 changes: 19 additions & 0 deletions docs/ref/filterset.txt
Original file line number Diff line number Diff line change
Expand Up @@ -199,3 +199,22 @@ filters for a model field, you can override ``filter_for_lookup()``. Ex::

# use default behavior otherwise
return super().filter_for_lookup(f, lookup_type)


.. _filterset_factory:

Using ``filterset_factory``
---------------------------

A ``FilterSet`` for a ``model`` can also be created by the
``filterset_factory``, which creats a ``FilterSet`` with the ``model`` set in
the FilterSets Meta. You can pass a customized ``FilterSet`` class to the
``filterset_factory``, which then uses this class a a base for the created
``FilterSet``. Ex::

class CustomFilterSet(django_filters.FilterSet):
class Meta:
form = CustomFilterSetForm


filterset = filterset_factory(Product, filterset=CustomFilterSet)
73 changes: 68 additions & 5 deletions tests/test_filterset.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
NumberFilter,
UUIDFilter,
)
from django_filters.filterset import FILTER_FOR_DBFIELD_DEFAULTS, FilterSet
from django_filters.filterset import FILTER_FOR_DBFIELD_DEFAULTS, FilterSet, filterset_factory
from django_filters.widgets import BooleanWidget

from .models import (
Expand Down Expand Up @@ -52,10 +52,6 @@ def test_get_declared_filters(self):
def test_filters_for_model(self):
pass

@unittest.skip("todo")
def test_filterset_factory(self):
pass


class DbFieldDefaultFiltersTests(TestCase):
def test_expected_db_fields_get_filters(self):
Expand Down Expand Up @@ -715,6 +711,73 @@ class F(A, B):
"f5": CharFilter,
}

def test_filterset_factory(self):
filterset = filterset_factory(Article)
self.assertEqual(list(filterset.base_filters), ["name", "published", "author"])

def test_filterset_factory_fields(self):
filterset = filterset_factory(Article, fields=["name"])
self.assertEqual(list(filterset.base_filters), ["name"])

def test_filterset_factory_base_filter(self):
class FilterSetBase(FilterSet):
f1 = CharFilter()
f2 = CharFilter()

filterset = filterset_factory(Article, filterset=FilterSetBase)
self.assertEqual(list(filterset.base_filters), ["name", "published", "author", "f1", "f2"])

def test_filterset_factory_base_filter_fields(self):
class FilterSetBase(FilterSet):
f1 = CharFilter()
f2 = CharFilter()

filterset = filterset_factory(Article, filterset=FilterSetBase, fields=["name"])
self.assertEqual(list(filterset.base_filters), ["name", "f1", "f2"])

def test_filterset_factory_base_filter_meta_fields(self):
class FilterSetBase(FilterSet):
class Meta:
fields = ["name"]
f1 = CharFilter()
f2 = CharFilter()

filterset = filterset_factory(Article, filterset=FilterSetBase)
self.assertEqual(list(filterset.base_filters), ["name", "f1", "f2"])

def test_filterset_factory_base_filter_fields_and_meta_fields(self):
class FilterSetBase(FilterSet):
class Meta:
fields = ["name"]
f1 = CharFilter()
f2 = CharFilter()

filterset = filterset_factory(Article, filterset=FilterSetBase, fields=["author"])
self.assertEqual(list(filterset.base_filters), ["author", "f1", "f2"])

def test_filtesret_factory_base_filter_meta_inheritance_filter_overrides(self):
class FilterSetBase(FilterSet):
class Meta:
filter_overrides = {
models.CharField: {
"filter_class": BooleanFilter,
},
}

filterset = filterset_factory(Article, FilterSetBase)

f = Article._meta.get_field("author")
result, params = filterset.filter_for_lookup(f, "isnull")
self.assertEqual(result, BooleanFilter)

def test_filtesret_factory_base_filter_meta_inheritance_exclude(self):
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you check the typos in the test method names please? (Same with test above too.)

class FilterSetBase(FilterSet):
class Meta:
exclude = ["published"]

filterset = filterset_factory(Article, FilterSetBase)
self.assertEqual(list(filterset.base_filters), ["name", "author"])


class FilterSetInstantiationTests(TestCase):
class F(FilterSet):
Expand Down