Skip to content

Commit

Permalink
fix(generic): 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,
therefore we are backporting the one we proposed on
carltongibson/django-filter#1636
  • Loading branch information
b1rger committed Jan 19, 2024
1 parent dae1a54 commit c235107
Showing 1 changed file with 12 additions and 18 deletions.
30 changes: 12 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 @@ -11,6 +12,9 @@ class GenericFilterSet(FilterSet):
See also: https://github.com/carltongibson/django-filter/issues/1630
"""

class Meta:
form = GenericFilterSetForm

@property
def form(self):
if not hasattr(self, "_form"):
Expand All @@ -24,22 +28,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 c235107

Please sign in to comment.