【问题标题】:graphene_django DjangoFilterConnectionField - The type * doesn't have a connectiongraphene_django DjangoFilterConnectionField - 类型 * 没有连接
【发布时间】:2021-10-25 10:38:25
【问题描述】:

我对 graphene_django 有疑问。当我创建一个 DjangoObjectType 并将其传递给我的查询中的 DjangoFilterConnectionField 时,我得到一个 AssertionError 类型 {my DjangoObjectType class} 没有连接。

这是我的代码的一部分:

class TradeType(DjangoObjectType):
    class Meta:
        model = Trade
        filter_fields = {
            'id': ['exact', 'range', 'in', 'gte', 'lt'],
            'updated': ['gte', 'lt'],
            'created': ['gte', 'lt']
        }
        interface = (graphene.relay.Node,)

class Query(graphene.ObjectType):
    trades = DjangoFilterConnectionField(TradeType)

所有这些都是我根据this page 完成的。

Exception in thread django-main-thread:

Traceback (most recent call last):

File "/usr/local/lib/python3.8/threading.py", line 932, in
_bootstrap_inner

self.run()

File "/usr/local/lib/python3.8/threading.py", line 870, in run

self._target(*self._args, **self._kwargs)

File "/usr/local/lib/python3.8/site-packages/django/utils/autoreload.py", line 64, in wrapper

fn(*args, **kwargs)

File "/usr/local/lib/python3.8/site-packages/channels/management/commands/runserver.py", line 69, in inner_run

self.check(display_num_errors=True)

File "/usr/local/lib/python3.8/site-packages/django/core/management/base.py", line 419, in check

all_issues = checks.run_checks(

File "/usr/local/lib/python3.8/site-packages/django/core/checks/registry.py", line 76, in run_checks

new_errors = check(app_configs=app_configs, databases=databases)

File "/usr/local/lib/python3.8/site-packages/django/core/checks/urls.py", line 13, in check_url_config

return check_resolver(resolver)

File "/usr/local/lib/python3.8/site-packages/django/core/checks/urls.py", line 23, in check_resolver

return check_method()

File "/usr/local/lib/python3.8/site-packages/django/urls/resolvers.py", line 412, in check

for pattern in self.url_patterns:

File "/usr/local/lib/python3.8/site-packages/django/utils/functional.py", line 48, in __get__

res = instance.__dict__[self.name] = self.func(instance)

File "/usr/local/lib/python3.8/site-packages/django/urls/resolvers.py", line 598, in url_patterns

patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)

File "/usr/local/lib/python3.8/site-packages/django/utils/functional.py", line 48, in __get__

res = instance.__dict__[self.name] = self.func(instance)

File "/usr/local/lib/python3.8/site-packages/django/urls/resolvers.py", line 591, in urlconf_module

return import_module(self.urlconf_name)

File "/usr/local/lib/python3.8/importlib/__init__.py", line 127, in import_module

return _bootstrap._gcd_import(name[level:], package, level)

File "<frozen importlib._bootstrap>", line 1014, in _gcd_import

File "<frozen importlib._bootstrap>", line 991, in _find_and_load

File "<frozen importlib._bootstrap>", line 975, in
_find_and_load_unlocked

File "<frozen importlib._bootstrap>", line 671, in _load_unlocked

File "<frozen importlib._bootstrap_external>", line 783, in exec_module

File "<frozen importlib._bootstrap>", line 219, in
_call_with_frames_removed

File "/app/tabdeal/urls.py", line 45, in <module>

path('utils/', include('utils.urls')),

File "/usr/local/lib/python3.8/site-packages/django/urls/conf.py", line 34, in include

urlconf_module = import_module(urlconf_module)

File "/usr/local/lib/python3.8/importlib/__init__.py", line 127, in import_module

return _bootstrap._gcd_import(name[level:], package, level)

File "<frozen importlib._bootstrap>", line 1014, in _gcd_import

File "<frozen importlib._bootstrap>", line 991, in _find_and_load

File "<frozen importlib._bootstrap>", line 975, in
_find_and_load_unlocked

File "<frozen importlib._bootstrap>", line 671, in _load_unlocked

File "<frozen importlib._bootstrap_external>", line 783, in exec_module

File "<frozen importlib._bootstrap>", line 219, in
_call_with_frames_removed

File "/app/utils/urls.py", line 6, in <module>

from utils.schema import schema

File "/app/utils/schema.py", line 200, in <module>

schema = graphene.Schema(query=Query)

File "/usr/local/lib/python3.8/site-packages/graphene/types/schema.py", line 78, in __init__

self.build_typemap()

File "/usr/local/lib/python3.8/site-packages/graphene/types/schema.py", line 167, in build_typemap

self._type_map = TypeMap(

File "/usr/local/lib/python3.8/site-packages/graphene/types/typemap.py", line 80, in __init__

super(TypeMap, self).__init__(types)

File "/usr/local/lib/python3.8/site-packages/graphql/type/typemap.py", line 31, in __init__

self.update(reduce(self.reducer, types, OrderedDict())) # type: ignore

File "/usr/local/lib/python3.8/site-packages/graphene/types/typemap.py", line 88, in reducer

return self.graphene_reducer(map, type)

File "/usr/local/lib/python3.8/site-packages/graphene/types/typemap.py", line 117, in graphene_reducer

return GraphQLTypeMap.reducer(map, internal_type)

File "/usr/local/lib/python3.8/site-packages/graphql/type/typemap.py", line 109, in reducer

field_map = type_.fields

File "/usr/local/lib/python3.8/site-packages/graphql/pyutils/cached_property.py", line 22, in __get__

value = obj.__dict__[self.func.__name__] = self.func(obj)

File "/usr/local/lib/python3.8/site-packages/graphql/type/definition.py", line 198, in fields

return define_field_map(self, self._fields)

File "/usr/local/lib/python3.8/site-packages/graphql/type/definition.py", line 212, in define_field_map

field_map = field_map()

File "/usr/local/lib/python3.8/site-packages/graphene/types/typemap.py", line 275, in construct_fields_for_type

map = self.reducer(map, field.type)

File "/usr/local/lib/python3.8/site-packages/graphene_django/fields.py", line 98, in type

assert _type._meta.connection, "The type {} doesn't have a connection".format(

AssertionError: The type TradeType doesn't have a connection

graphene==2.1.9,graphene_django==2.15,django-filter==2.4

【问题讨论】:

    标签: django graphene-django


    【解决方案1】:

    我认为问题在于找不到要使用的接口,因为您在 Meta 类中将 interfaces 错误输入为 interface

    class TradeType(DjangoObjectType):
        class Meta:
            ...
            interfaces = (graphene.relay.Node,)
    
    

    【讨论】:

      猜你喜欢
      • 2019-03-15
      • 2020-10-17
      • 2011-02-13
      • 1970-01-01
      • 2021-06-05
      • 1970-01-01
      • 2014-10-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多