Skip to content

Commit e4cf59e

Browse files
authored
Handle isnull filters differently (#753)
* Handle isnull filters differently * Change to rsplit
1 parent a64ba65 commit e4cf59e

File tree

2 files changed

+62
-5
lines changed

2 files changed

+62
-5
lines changed

graphene_django/filter/tests/test_fields.py

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,6 @@ class Meta:
5656
model = Pet
5757
interfaces = (Node,)
5858

59-
# schema = Schema()
60-
6159

6260
def get_args(field):
6361
return field.args
@@ -820,6 +818,58 @@ class Query(ObjectType):
820818
)
821819

822820

821+
def test_other_filter_types():
822+
class PetType(DjangoObjectType):
823+
class Meta:
824+
model = Pet
825+
interfaces = (Node,)
826+
filter_fields = {"age": ["exact", "isnull", "lt"]}
827+
fields = ("age",)
828+
829+
class Query(ObjectType):
830+
pets = DjangoFilterConnectionField(PetType)
831+
832+
schema = Schema(query=Query)
833+
834+
assert str(schema) == dedent(
835+
"""\
836+
schema {
837+
query: Query
838+
}
839+
840+
interface Node {
841+
id: ID!
842+
}
843+
844+
type PageInfo {
845+
hasNextPage: Boolean!
846+
hasPreviousPage: Boolean!
847+
startCursor: String
848+
endCursor: String
849+
}
850+
851+
type PetType implements Node {
852+
age: Int!
853+
id: ID!
854+
}
855+
856+
type PetTypeConnection {
857+
pageInfo: PageInfo!
858+
edges: [PetTypeEdge]!
859+
}
860+
861+
type PetTypeEdge {
862+
node: PetType
863+
cursor: String!
864+
}
865+
866+
type Query {
867+
pets(before: String, after: String, first: Int, last: Int, age: Int, age_Isnull: Boolean, age_Lt: Int): PetTypeConnection
868+
}
869+
"""
870+
)
871+
872+
823873
def test_filter_filterset_based_on_mixin():
824874
class ArticleFilterMixin(FilterSet):
825875
@classmethod

graphene_django/filter/utils.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,16 @@ def get_filtering_args_from_filterset(filterset_class, type):
1818
if name in filterset_class.declared_filters:
1919
form_field = filter_field.field
2020
else:
21-
field_name = name.split("__", 1)[0]
22-
23-
if hasattr(model, field_name):
21+
try:
22+
field_name, filter_type = name.rsplit("__", 1)
23+
except ValueError:
24+
field_name = name
25+
filter_type = None
26+
27+
# If the filter type is `isnull` then use the filter provided by
28+
# DjangoFilter (a BooleanFilter).
29+
# Otherwise try and get a filter based on the actual model field
30+
if filter_type != "isnull" and hasattr(model, field_name):
2431
model_field = model._meta.get_field(field_name)
2532

2633
if hasattr(model_field, "formfield"):

0 commit comments

Comments
 (0)