fix: declaration of required variable in filters v3 (#1137)

* fix: declaration of required variable

* Add unit test

* Fix formating

Co-authored-by: Thomas Leonard <thomas@loftorbital.com>
This commit is contained in:
Thomas Leonard 2021-02-26 23:44:43 +01:00 committed by GitHub
parent ea593b673f
commit 6046a710c8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 3 deletions

View File

@ -16,7 +16,7 @@ pytestmark = []
if DJANGO_FILTER_INSTALLED:
import django_filters
from django_filters import FilterSet, NumberFilter
from django_filters import FilterSet, NumberFilter, OrderingFilter
from graphene_django.filter import (
GlobalIDFilter,
@ -1275,3 +1275,22 @@ def test_filter_string_contains():
assert result.data == {
"people": {"edges": [{"node": {"name": "Joe"}}, {"node": {"name": "Bob"}},]}
}
def test_only_custom_filters():
class ReporterFilter(FilterSet):
class Meta:
model = Reporter
fields = []
some_filter = OrderingFilter(fields=("name",))
class ReporterFilterNode(DjangoObjectType):
class Meta:
model = Reporter
interfaces = (Node,)
fields = "__all__"
filterset_class = ReporterFilter
field = DjangoFilterConnectionField(ReporterFilterNode)
assert_arguments(field, "some_filter")

View File

@ -37,6 +37,7 @@ def get_filtering_args_from_filterset(filterset_class, type):
registry = type._meta.registry
for name, filter_field in filterset_class.base_filters.items():
filter_type = filter_field.lookup_expr
required = filter_field.extra.get("required", False)
field_type = None
form_field = None
@ -47,8 +48,6 @@ def get_filtering_args_from_filterset(filterset_class, type):
or isinstance(filter_field, ArrayFilter)
):
# Get the filter field for filters that are no explicitly declared.
required = filter_field.extra.get("required", False)
if filter_type == "isnull":
field = graphene.Boolean(required=required)
else: