【问题标题】:Problem importing module for GRAPHENE setting 'SCHEMA' in Django 2.2在 Django 2.2 中为 GRAPHENE 设置“SCHEMA”导入模块时出现问题
【发布时间】:2019-12-09 20:36:24
【问题描述】:

我是 django(在我的 virtualenv 中安装了 2.2 版)和 graphql 的新手。我尝试按照以下站点的说明在 django 中设置 graphql

https://docs.graphene-python.org/projects/django/en/latest/installation/

当我尝试使用 url http://127.0.0.1:8000/graphql/ 运行服务器时

我收到以下错误。

ImportError at /graphql/
Could not import 'django_root.schema.schema' for Graphene setting 
'SCHEMA'. ModuleNotFoundError: No module named 'django_root'.

我仔细按照说明进行操作,但无法做到这一点。请帮忙。我检查了类似的问题,但没有帮助。

编辑: 我尝试了以下

'SCHEMA': 'folder_with_setting.py_file.schema.schema'

'SCHEMA': 'folder_with_manage.py_file.schema.schema'

'SCHEMA': 'folder_with_manage.py_file.folder_with_settings.py_file.schema.schema'

'SCHEMA': 'folder_with_manage.py_file/folder_with_settings.py_file.schema.schema'

和许多其他组合。它不工作。

【问题讨论】:

    标签: python django graphql


    【解决方案1】:

    你应该在settings.py中使用你项目的正确路径

    GRAPHENE = {
        'SCHEMA': 'django_root.schema.schema'  #  change your path
    }
    
    

    其中 path.schema.schema 是 Django 项目中 Schema 对象的位置。

    【讨论】:

    • 我应该像在 BASE_DIR 中一样将“os.path.dirname(os.path.dirname(os.path.abspath(file)))”放在设置.py ?
    • 先试试绝对路径还是相对路径
    • 好的,我应该指向文件 schema.py 还是只指向文件夹?
    【解决方案2】:
    'SCHEMA': 'addyourappnamehere.schema.schema',
    

    遇到同样的错误 这行得通 我添加了应用名称 例如:

    'SCHEMA': 'auth.schema.schema',
    

    【讨论】:

      【解决方案3】:

      问题是您必须解决schema.py 所在的路径。例如,您拥有的应用名称类似于以下项目大纲:

      django_project
      ├── app_name
      │   ├── model.py
      │   ├── schema.py
      │   └── ...
      ├── django_project
      │   ├── settings.py
      │   └── ...
      └── ...
      

      现在进入settings.py添加以下行:

      INSTALLED_APPS = (
          # ...
          'app_name',
          'django.contrib.staticfiles', # Required for GraphiQL
          'graphene_django',
      )
      
      GRAPHENE = {
          'SCHEMA': 'app_name.schema.schema' # Where your Graphene schema lives
      }
      

      [注意]:

      你也可以关注this instruction一步一步来。

      【讨论】:

        猜你喜欢
        • 2021-11-10
        • 2011-02-19
        • 1970-01-01
        • 2016-02-06
        • 1970-01-01
        • 1970-01-01
        • 2014-05-30
        • 2018-06-24
        • 1970-01-01
        相关资源
        最近更新 更多