【问题标题】:How to solve "Could not import 'todo.schema.schema' for Graphene setting 'SCHEMA'. AttributeError: module 'graphene' has no attribute 'string'."?如何解决“无法为石墨烯设置'SCHEMA'导入'todo.schema.schema'。AttributeError:模块'graphene'没有属性'string'。”?
【发布时间】:2021-11-10 03:06:14
【问题描述】:

我创建了 Django 石墨烯项目。突然我收到一个错误无法为石墨烯设置“SCHEMA”导入“todo.schema.schema”。 AttributeError: module 'graphene' has no attribute 'string'。但我不知道如何解决。

我的架构结构是: 待办事项/模式/模式

seting.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'
}

主 urls.py:

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

schema.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()

我错过了什么?还是我做错了什么?

【问题讨论】:

    标签: python python-3.x django graphene-python graphene-django


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-03-19
      • 2021-10-15
      • 2020-09-07
      • 2021-12-27
      • 2022-06-21
      • 2022-07-18
      • 2018-01-29
      相关资源
      最近更新 更多