diff --git a/graphene_django/filter/tests/test_fields.py b/graphene_django/filter/tests/test_fields.py index aa6a903..1ffa0f4 100644 --- a/graphene_django/filter/tests/test_fields.py +++ b/graphene_django/filter/tests/test_fields.py @@ -56,8 +56,6 @@ if DJANGO_FILTER_INSTALLED: model = Pet interfaces = (Node,) - # schema = Schema() - def get_args(field): return field.args @@ -820,6 +818,58 @@ def test_integer_field_filter_type(): ) +def test_other_filter_types(): + class PetType(DjangoObjectType): + class Meta: + model = Pet + interfaces = (Node,) + filter_fields = {"age": ["exact", "isnull", "lt"]} + fields = ("age",) + + class Query(ObjectType): + pets = DjangoFilterConnectionField(PetType) + + schema = Schema(query=Query) + + assert str(schema) == dedent( + """\ + schema { + query: Query + } + + interface Node { + id: ID! + } + + type PageInfo { + hasNextPage: Boolean! + hasPreviousPage: Boolean! + startCursor: String + endCursor: String + } + + type PetType implements Node { + age: Int! + id: ID! + } + + type PetTypeConnection { + pageInfo: PageInfo! + edges: [PetTypeEdge]! + } + + type PetTypeEdge { + node: PetType + cursor: String! + } + + type Query { + pets(before: String, after: String, first: Int, last: Int, age: Int, age_Isnull: Boolean, age_Lt: Int): PetTypeConnection + } + """ + ) + + def test_filter_filterset_based_on_mixin(): class ArticleFilterMixin(FilterSet): @classmethod diff --git a/graphene_django/filter/utils.py b/graphene_django/filter/utils.py index 81efb63..abb03a9 100644 --- a/graphene_django/filter/utils.py +++ b/graphene_django/filter/utils.py @@ -18,9 +18,16 @@ def get_filtering_args_from_filterset(filterset_class, type): if name in filterset_class.declared_filters: form_field = filter_field.field else: - field_name = name.split("__", 1)[0] + try: + field_name, filter_type = name.rsplit("__", 1) + except ValueError: + field_name = name + filter_type = None - if hasattr(model, field_name): + # 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"):