GraphQL cookbook examp中的Django筛选器错误

2024-05-08 22:18:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我目前正在通过graphene教程学习GraphQL,但是当我尝试运行时收到了以下错误消息:

python3 manage.py graphql_schema

错误:

^{pr2}$

在我的配料里/架构.py公司名称:

# cookbook/ingredients/schema.py
from graphene import relay, ObjectType, AbstractType
from graphene_django import DjangoObjectType
from graphene_django.filter import DjangoFilterConnectionField

from .models import Category, Ingredient

# Graphene will automatically map the Category model's fields onto the CategoryNode.
# This is configured in the CategoryNode's Meta class (as you can see below)
class CategoryNode(DjangoObjectType):
    class Meta:
        model = Category
        filter_fields = ['name', 'ingredients']
        filter_order_by_field = ['name']
        interfaces = (relay.Node, )


class IngredientNode(DjangoObjectType):
    class Meta:
        model = Ingredient
        # Allow for some more advanced filtering here
        filter_fields = {
            'name': ['exact', 'icontains', 'istartswith'],
            'notes': ['exact', 'icontains'],
            'category': ['exact'],
            'category__name': ['exact'],
        }
        order_by_field = ['name', 'category__name']
        interfaces = (relay.Node, )


class Query(AbstractType):
    category = relay.Node.Field(CategoryNode)
    all_categories = DjangoFilterConnectionField(CategoryNode)

    ingredient = relay.Node.Field(IngredientNode)
    all_ingredients = DjangoFilterConnectionField(IngredientNode)

django过滤器版本0.15.3

我试图从官方的石墨烯回购协议中复制这个例子,它在python2.7上运行得很好。但是在python3.5中,相同的代码无法工作。我怀疑django过滤器中发生了一些改变,导致了不兼容。在python2.7中,django过滤器的版本是0.11.0。有人知道如何在0.15.3中使用它吗?在


Tags: djangonamefrompyimportnodefilterrelay