Skip to content

Handle isnull filters differently #753

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 52 additions & 2 deletions graphene_django/filter/tests/test_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,6 @@ class Meta:
model = Pet
interfaces = (Node,)

# schema = Schema()


def get_args(field):
return field.args
Expand Down Expand Up @@ -820,6 +818,58 @@ class Query(ObjectType):
)


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
Expand Down
13 changes: 10 additions & 3 deletions graphene_django/filter/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]

if hasattr(model, field_name):
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"):
Expand Down