Skip to content

Commit

Permalink
Merge 0e7fca8 into 4f21750
Browse files Browse the repository at this point in the history
  • Loading branch information
jkimbo committed Sep 22, 2019
2 parents 4f21750 + 0e7fca8 commit 9133f8b
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 5 deletions.
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

0 comments on commit 9133f8b

Please sign in to comment.