Skip to content

Commit c8a56f8

Browse files
authored
Allow string references in DjangoListField (#885)
* Allow passing string references to DjangoListField * Refactor logic to work with string imports
1 parent 348fcf3 commit c8a56f8

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

graphene_django/fields.py

+12-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from functools import partial
22

3+
import six
34
from django.db.models.query import QuerySet
45
from graphql_relay.connection.arrayconnection import connection_from_list_slice
56
from promise import Promise
@@ -19,19 +20,23 @@ def __init__(self, _type, *args, **kwargs):
1920
if isinstance(_type, NonNull):
2021
_type = _type.of_type
2122

23+
# Django would never return a Set of None vvvvvvv
24+
super(DjangoListField, self).__init__(List(NonNull(_type)), *args, **kwargs)
25+
2226
assert issubclass(
23-
_type, DjangoObjectType
27+
self._underlying_type, DjangoObjectType
2428
), "DjangoListField only accepts DjangoObjectType types"
2529

26-
# Django would never return a Set of None vvvvvvv
27-
super(DjangoListField, self).__init__(List(NonNull(_type)), *args, **kwargs)
30+
@property
31+
def _underlying_type(self):
32+
_type = self._type
33+
while hasattr(_type, "of_type"):
34+
_type = _type.of_type
35+
return _type
2836

2937
@property
3038
def model(self):
31-
_type = self.type.of_type
32-
if isinstance(_type, NonNull):
33-
_type = _type.of_type
34-
return _type._meta.model
39+
return self._underlying_type._meta.model
3540

3641
@staticmethod
3742
def list_resolver(django_object_type, resolver, root, info, **args):

graphene_django/tests/test_fields.py

+6
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ class TestType(ObjectType):
1919
with pytest.raises(AssertionError):
2020
list_field = DjangoListField(TestType)
2121

22+
def test_only_import_paths(self):
23+
list_field = DjangoListField("graphene_django.tests.schema.Human")
24+
from .schema import Human
25+
26+
assert list_field._type.of_type.of_type is Human
27+
2228
def test_non_null_type(self):
2329
class Reporter(DjangoObjectType):
2430
class Meta:

0 commit comments

Comments
 (0)