django-filter: resolve field along with lookup expression to pro… (#805)

* django-filter: resolve field along with lookup expression to properly resolve field

* bring back django-filter with method test

* remove dangling comment

* refactor based on better knowledge of django-filters
This commit is contained in:
Jason Kraus 2019-11-28 02:48:03 -08:00 committed by Jonathan Kim
parent e51e60209a
commit 3ce44908c9

View File

@ -1,5 +1,6 @@
import six
from django_filters.utils import get_model_field
from .filterset import custom_filterset_factory, setup_filterset
@ -18,22 +19,12 @@ def get_filtering_args_from_filterset(filterset_class, type):
if name in filterset_class.declared_filters:
form_field = filter_field.field
else:
try:
field_name, filter_type = name.rsplit("__", 1)
except ValueError:
field_name = name
filter_type = None
# If the filter type is `isnull` then use the filter provided by
# DjangoFilter (a BooleanFilter).
# Otherwise try and get a filter based on the actual model field
if filter_type != "isnull" and hasattr(model, field_name):
model_field = model._meta.get_field(field_name)
if hasattr(model_field, "formfield"):
form_field = model_field.formfield(
required=filter_field.extra.get("required", False)
)
model_field = get_model_field(model, filter_field.field_name)
filter_type = filter_field.lookup_expr
if filter_type != "isnull" and hasattr(model_field, "formfield"):
form_field = model_field.formfield(
required=filter_field.extra.get("required", False)
)
# Fallback to field defined on filter if we can't get it from the
# model field