首页 > 解决方案 > 如何解决“无法为石墨烯设置'SCHEMA'导入'todo.schema.schema'。AttributeError:模块'graphene'没有属性'string'。”?

问题描述

我创建了 Django 石墨烯项目。突然我收到一个错误,无法为石墨烯设置“SCHEMA”导入“todo.schema.schema”。AttributeError:模块“石墨烯”没有属性“字符串”。但我不知道如何解决它。

我的架构结构是: todo/schema/schema

设置.py:

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

    # third party app
    'graphene_django',
    'django_filters',
]

GRAPHENE = {
    'SCHEMA': 'todo.schema.schema'
}

主要网址.py:

urlpatterns = [
    path('graphql/', csrf_exempt(GraphQLView.as_view(graphiql=True))),
]

架构.py:

class Query(TodoQuery, graphene.ObjectType):
    pass


class Mutation(Mutation, graphene.ObjectType):
    pass


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

应用程序架构.py:

# declar todo model field
class TodoType(DjangoObjectType):
    class Meta:
        model = TodoList
        fields = ('id', 'title', 'date', 'text')


# declar user model filed
class UserType(DjangoObjectType):
    class Meta:
        model = User


# todo list query
class TodoQuery(graphene.ObjectType):
    todoList = DjangoListField(TodoType)

    def resolve_todoList(root, info):
        return TodoList.objects.filter(userId=2)


# create todo
class TodoCreate(graphene.Mutation):
    class Arguments:
        title = graphene.String(Required=True)
        text = graphene.string(Required=True)

    todo = graphene.Field(TodoType)

    def mutate(root, info, title, text):
        # userId = info.context.user
        user = User.objects.get(id=2)
        todo = TodoList(userId=user, title=title, text=text)
        todo.save()
        return TodoCreate(todo=todo)


# todo mutation
class Mutation(graphene.ObjectType):
    createTodo = TodoCreate.Field()

我错过了什么?或者我做错了什么?

标签: pythonpython-3.xdjangographene-pythongraphene-django

解决方案


你有一个错字text = graphene.string(Required=True)。它还在错误中说石墨烯没有属性字符串。它区分大小写,应该是text = graphene.String(Required=True)你可以为你的编辑器使用一个 linter,这样你将来就可以捕捉到这样的小东西。


推荐阅读