Skip to content

Commit

Permalink
fix: backport filterset_factory from django-filters
Browse files Browse the repository at this point in the history
The custom implementation did not allow to use custom Meta attributes
  • Loading branch information
b1rger committed Jan 19, 2024
1 parent dae1a54 commit 6c3b18f
Showing 1 changed file with 11 additions and 18 deletions.
29 changes: 11 additions & 18 deletions apis_core/generic/filtersets.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from django_filters.filterset import FilterSet
from django_filters.constants import ALL_FIELDS
from .forms import GenericFilterSetForm


Expand All @@ -10,6 +11,8 @@ class GenericFilterSet(FilterSet):
field.
See also: https://github.com/carltongibson/django-filter/issues/1630
"""
class Meta:
form = GenericFilterSetForm

@property
def form(self):
Expand All @@ -24,22 +27,12 @@ def form(self):
return self._form


def filterset_factory(model, filterset=GenericFilterSet, fields="__all__"):
"""
A custom filterset_factory, because we want to be a able to set the
filterset as well as the `form` attribute of the filterset
This can hopefully be removed once
https://github.com/carltongibson/django-filter/issues/1631 is implemented.
"""

meta = type(
str("Meta"),
(object,),
{"model": model, "fields": fields, "form": GenericFilterSetForm},
)
filterset = type(
str("%sFilterSet" % model._meta.object_name),
(filterset,),
{"Meta": meta},
# This is a backport from https://github.com/carltongibson/django-filter/pull/1636
# It can be removed once that is merged and released
def filterset_factory(model, filterset=FilterSet, fields=ALL_FIELDS):
attrs = {"model": model, "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

0 comments on commit 6c3b18f

Please sign in to comment.