Keep original queryset on DjangoFilterConnectionField (#816)

* Keep original queryset on DjangoFilterConnectionField

The PR #796 broke DjangoFilterConnectionField making it always get the
raw queryset from the model to apply the filters in it.

This makes sure that the DjangoObjectType's .get_queryset is called,
keeping any filtering it might have made.

* Add regression test
This commit is contained in:
Thiago Bellini Ribeiro 2019-11-29 06:13:16 -03:00 committed by Jonathan Kim
parent e82a2d75c6
commit 7e7f18ee0e
2 changed files with 42 additions and 3 deletions

View File

@ -55,10 +55,11 @@ class DjangoFilterConnectionField(DjangoConnectionField):
def resolve_queryset(
cls, connection, iterable, info, args, filtering_args, filterset_class
):
qs = super(DjangoFilterConnectionField, cls).resolve_queryset(
connection, iterable, info, args
)
filter_kwargs = {k: v for k, v in args.items() if k in filtering_args}
return filterset_class(
data=filter_kwargs, queryset=iterable, request=info.context
).qs
return filterset_class(data=filter_kwargs, queryset=qs, request=info.context).qs
def get_queryset_resolver(self):
return partial(

View File

@ -756,6 +756,44 @@ def test_annotation_with_only():
assert result.data == expected
def test_node_get_queryset_is_called():
class ReporterType(DjangoObjectType):
class Meta:
model = Reporter
interfaces = (Node,)
filter_fields = ()
@classmethod
def get_queryset(cls, queryset, info):
return queryset.filter(first_name="b")
class Query(ObjectType):
all_reporters = DjangoFilterConnectionField(
ReporterType, reverse_order=Boolean()
)
Reporter.objects.create(first_name="b")
Reporter.objects.create(first_name="a")
schema = Schema(query=Query)
query = """
query NodeFilteringQuery {
allReporters(first: 10) {
edges {
node {
firstName
}
}
}
}
"""
expected = {"allReporters": {"edges": [{"node": {"firstName": "b"}}]}}
result = schema.execute(query)
assert not result.errors
assert result.data == expected
def test_integer_field_filter_type():
class PetType(DjangoObjectType):
class Meta: