From 6046a710c887bf45a9480a162e20038ab5c95623 Mon Sep 17 00:00:00 2001 From: Thomas Leonard <64223923+tcleonard@users.noreply.github.com> Date: Fri, 26 Feb 2021 23:44:43 +0100 Subject: [PATCH] fix: declaration of required variable in filters v3 (#1137) * fix: declaration of required variable * Add unit test * Fix formating Co-authored-by: Thomas Leonard --- graphene_django/filter/tests/test_fields.py | 21 ++++++++++++++++++++- graphene_django/filter/utils.py | 3 +-- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/graphene_django/filter/tests/test_fields.py b/graphene_django/filter/tests/test_fields.py index 274f6ac..17b4630 100644 --- a/graphene_django/filter/tests/test_fields.py +++ b/graphene_django/filter/tests/test_fields.py @@ -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") diff --git a/graphene_django/filter/utils.py b/graphene_django/filter/utils.py index d4fc1bf..9922cc8 100644 --- a/graphene_django/filter/utils.py +++ b/graphene_django/filter/utils.py @@ -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: