Handle isnull filters differently (#753)

* Handle isnull filters differently

* Change to rsplit
This commit is contained in:
Jonathan Kim 2019-09-22 21:14:59 +01:00 committed by GitHub
parent a64ba65bef
commit e4cf59ecec
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 61 additions and 4 deletions

View File

@ -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

View File

@ -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"):