From c235107154ab50a1e90b9a5f1aba8d324fdc0df9 Mon Sep 17 00:00:00 2001 From: Birger Schacht Date: Fri, 19 Jan 2024 13:45:57 +0100 Subject: [PATCH] fix(generic): backport `filterset_factory` from django-filters The custom implementation did not allow to use custom Meta attributes, therefore we are backporting the one we proposed on https://github.com/carltongibson/django-filter/pull/1636 --- apis_core/generic/filtersets.py | 30 ++++++++++++------------------ 1 file changed, 12 insertions(+), 18 deletions(-) diff --git a/apis_core/generic/filtersets.py b/apis_core/generic/filtersets.py index 980a93172..e4ccdf925 100644 --- a/apis_core/generic/filtersets.py +++ b/apis_core/generic/filtersets.py @@ -1,4 +1,5 @@ from django_filters.filterset import FilterSet +from django_filters.constants import ALL_FIELDS from .forms import GenericFilterSetForm @@ -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"): @@ -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