/graphql 处的 Django ImportError

Posted

技术标签:

【中文标题】/graphql 处的 Django ImportError【英文标题】:Django ImportError at /graphql 【发布时间】:2018-04-26 07:29:51 【问题描述】:

根本不明白出了什么问题:无法为石墨烯设置“SCHEMA”导入“ss.schema.schema”。 AttributeError:类型对象“Query”没有属性“_meta”。

这是我在应用程序文件“pages.schema.py”中的查询类:

class Query(graphene.AbstractType):
    user = graphene.relay.Node.Field(UserNode)
    users = DjangoFilterConnectionField(UserNode, filterset_class=UserFilter)

这是根架构文件“ss.schema.py::”的完整内容

import graphene

import pages.schema


class Query(pages.schema.Query, graphene.ObjectType):
    pass


class Mutation(pages.schema.Mutation, graphene.ObjectType):
    pass


schema = graphene.Schema(query=Query, mutation=Mutation)

这是完整的回溯:

Environment:


Request Method: GET
Request URL: http://95.213.203.11/graphql

Django Version: 1.11.5
Python Version: 3.5.2
Installed Applications:
['django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'corsheaders',
 'ws4redis',
 'graphene_django',
 'pages']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'corsheaders.middleware.CorsMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']



Traceback:

File "/root/ss/lib/python3.5/site-packages/graphene_django/settings.py" in import_from_string
  74.         module = importlib.import_module(module_path)

File "/root/ss/lib/python3.5/importlib/__init__.py" in import_module
  126.     return _bootstrap._gcd_import(name[level:], package, level)

File "./ss/schema.py" in <module>
  3. import pages.schema

File "./pages/schema.py" in <module>
  130. schema = graphene.Schema(query=Query, mutation=Mutation)

File "/root/ss/lib/python3.5/site-packages/graphene/types/schema.py" in __init__
  27.         ).format(query)

File "/root/ss/lib/python3.5/site-packages/graphene/utils/subclass_with_meta.py" in __repr__
  11.         return cls._meta.name

During handling of the above exception (type object 'Query' has no attribute '_meta'), another exception occurred:

File "/root/ss/lib/python3.5/site-packages/django/core/handlers/exception.py" in inner
  41.             response = get_response(request)

File "/root/ss/lib/python3.5/site-packages/django/core/handlers/base.py" in _get_response
  187.                 response = self.process_exception_by_middleware(e, request)

File "/root/ss/lib/python3.5/site-packages/django/core/handlers/base.py" in _get_response
  185.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/root/ss/lib/python3.5/site-packages/django/views/decorators/csrf.py" in wrapped_view
  58.         return view_func(*args, **kwargs)

File "/root/ss/lib/python3.5/site-packages/django/views/generic/base.py" in view
  62.             self = cls(**initkwargs)

File "/root/ss/lib/python3.5/site-packages/graphene_django/views.py" in __init__
  70.             schema = graphene_settings.SCHEMA

File "/root/ss/lib/python3.5/site-packages/graphene_django/settings.py" in __getattr__
  116.             val = perform_import(val, attr)

File "/root/ss/lib/python3.5/site-packages/graphene_django/settings.py" in perform_import
  60.         return import_from_string(val, setting_name)

File "/root/ss/lib/python3.5/site-packages/graphene_django/settings.py" in import_from_string
  78.         raise ImportError(msg)

Exception Type: ImportError at /graphql
Exception Value: Could not import 'ss.schema.schema' for Graphene setting 'SCHEMA'. AttributeError: type object 'Query' has no attribute '_meta'.

有什么建议可以解决吗?

【问题讨论】:

能否提供您ss.schema.py的全部内容? @ClémentDenoix 好的。完成了。 谢谢。我在您的 ss.shema.py 中发现了一个问题。导入行import pages.schema 与同一文件底部定义的shema 变量发生冲突。 @ClémentDenoix 谢谢,你是对的。然而,在更正之后,问题发生了一些变化:无法为石墨烯设置“SCHEMA”导入“ss.schema.schema”。 AttributeError:“NoneType”对象没有属性“_meta”。 您能否根据您所做的更改更新 shema 文件? 【参考方案1】:

Graphene(以及 Django 和 SQLAlchemy 等类似框架)让我们轻松构建自己的 架构和合并程序逻辑,无需编写大量样板,有一个黑暗的一面: 他们给出的错误消息和回溯通常几乎无法将我们指向实际 错误。

这里就是这样。你ss/schema.py没有问题,问题一定出在 页面/schema.py。既然你给我们的东西这么少,我不知道是什么,但这里有一点 充实的示例,基于您的代码,适用于 Graphene 2.0:

ss/schema.py:

import graphene

import pages.schema

class Query(pages.schema.Query, graphene.ObjectType):
    pass

#class Mutation(pages.schema.Mutation, graphene.ObjectType):
#    pass

#schema = graphene.Schema(query=Query, mutation=Mutation)
schema = graphene.Schema(query=Query)

pages/models.py:

from django.db import models

class User(models.Model):
    name = models.CharField(max_length=100)

pages/schema.py:

from django_filters import FilterSet
from graphene import relay
from graphene_django import DjangoObjectType
from graphene_django.filter import DjangoFilterConnectionField

from .models import User

class UserNode(DjangoObjectType):
    class Meta:
        model = User
        interfaces = (relay.Node, )

class UserFilter(FilterSet):
    class Meta:
        model = User
        fields = ['name']

class Query(object):
    user = relay.Node.Field(UserNode)
    users = DjangoFilterConnectionField(UserNode, filterset_class=UserFilter)

使用以下查询进行测试:


  users 
    edges 
      node 
        id
        ... on UserNode 
          name
        
      
    
  

希望其中的某些内容能够为您指明正确的方向。如果没有,请发布更多页面/schema.py,我们将从那里开始。

【讨论】:

【参考方案2】:

这可能与您的项目/文件查询与突变导入结构的整体结构有关。 https://github.com/graphql-python/graphene-django/issues/569#issuecomment-883172665

以前有过

from orders.graphql.mutation.customer import CMutation
from orders.graphql.mutation.store import SMutation
from orders.graphql.query import CQuery, SQuery

在我的 schema.py 中。并得到了错误

ImportError: Could not import 'p.schema' for Graphene setting 'SCHEMA'. AttributeError: 'NoneType' object has no attribute '_meta'.

之后我把它改成了

from orders.graphql.query import CQuery, SQuery
from orders.graphql.mutation.customer import CMutation
from orders.graphql.mutation.store import SMutation

问题解决了。

【讨论】:

以上是关于/graphql 处的 Django ImportError的主要内容,如果未能解决你的问题,请参考以下文章

/graphql 处的递归错误。超出最大递归深度

需要向 GraphQLView 提供 Schema

Askbot 安装 - /questions/ 处的 TemplateSyntaxError 无效块标签:'import'

GraphQl,Sequelize-CLi,模型捆绑器 - sequelize.import 不是函数

忘记了 django 和 graphql 的密码?

Django 1.9.4 中 /admin/ 处的 ImportError