[英]GraphQL AttributeError: module 'graphene' has no attribute 'Heroes'
i am a beginner with Django & GraphQL, i had a problem the at first step, i can not reach to GraphiQL, i had an error我是 Django 和 GraphQL 的初学者,我在第一步遇到问题,我无法访问 GraphiQL,我遇到了错误
Could not import 'traindjango.schema.schema' for Graphene setting 'SCHEMA'.
AttributeError: module 'graphene' has no attribute 'Heroes'.
traindjango/heroes/schema.py 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 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 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 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)
Could you please help me what i can do it?你能帮我做什么吗?
Thanx a lot非常感谢
Instead of代替
heroes = graphene.Heroes(HeroesType)
You need你需要
heroes = graphene.List(HeroesType)
Heroes
is your model, and does not belong to Graphene. Heroes
是你的model,不属于石墨烯。
Then you need to rename resolve_links
as resolve_heroes
然后你需要将resolve_links
重命名为resolve_heroes
PS Good practice is to name your django modules in the singular, ie Hero, not Heroes. PS 好的做法是以单数命名您的 django 模块,即 Hero,而不是 Heroes。 You can set the verbose name for the plural in Meta
if it's not just adding an 's'.如果不只是添加“s”,您可以在Meta
中为复数设置详细名称。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.