0

我是 Django 和 GraphQL 的初学者,我在第一步遇到问题,我无法访问 GraphiQL,我遇到了错误

Could not import 'traindjango.schema.schema' for Graphene setting 'SCHEMA'.
AttributeError: module 'graphene' has no attribute 'Heroes'.

traindjango/heroes/schema.py

import graphene
from graphene_django import DjangoObjectType

from .models import Heroes


class HeroesType(DjangoObjectType):
    class Meta:
        model = Heroes


class Query(graphene.ObjectType):
    heroes = graphene.Heroes(HeroesType)

    def resolve_links(self, info, **kwargs):
        return Heroes.objects.all()

traindjango/traindjango/schema.py

import graphene

import heroes.schema


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


schema = graphene.Schema(query=Query)

traindjango/traindjango/settings.py

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'heroes',
    'graphene_django',
]

GRAPHENE = {
    'SCHEMA' : 'traindjango.schema.schema',
}

traindjango/heroes/models.py

from django.db import models

class Heroes(models.Model):
    name  = models.CharField(max_length=100, verbose_name='Name') 
    power = models.IntegerField(default=0)
    city  = models.TextField(max_length=100, verbose_name='City' ,null=True, blank=True)

你能帮我做什么吗?

非常感谢

4

1 回答 1

1

代替

heroes = graphene.Heroes(HeroesType)

你需要

heroes = graphene.List(HeroesType)

Heroes是你的模型,不属于石墨烯。

然后你需要重命名resolve_linksresolve_heroes

PS 好的做法是用单数命名你的 django 模块,即 Hero,而不是 Heroes。Meta如果不只是添加“s”,您可以为复数设置详细名称。

于 2019-10-30T13:23:29.137 回答