3
我目前正在通过石墨烯教程学习GraphQL。在GraphQL食谱示例中的Django过滤器错误
但是我遇到了这个错误,当我试图运行
错误:
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/core/management/__init__.py", line 354, in execute_from_command_line
utility.execute()
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/core/management/__init__.py", line 346, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/core/management/base.py", line 382, in run_from_argv
parser = self.create_parser(argv[0], argv[1])
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/core/management/base.py", line 355, in create_parser
self.add_arguments(parser)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/graphene_django/management/commands/graphql_schema.py", line 39, in add_arguments
default=graphene_settings.SCHEMA,
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/graphene_django/settings.py", line 110, in __getattr__
val = perform_import(val, attr)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/graphene_django/settings.py", line 54, in perform_import
return import_from_string(val, setting_name)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/graphene_django/settings.py", line 68, in import_from_string
module = importlib.import_module(module_path)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 986, in _gcd_import
File "<frozen importlib._bootstrap>", line 969, in _find_and_load
File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 662, in exec_module
File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
File "/Users/lebeier/Documents/cookbook/schema.py", line 3, in <module>
import ingredients.schema
File "/Users/lebeier/Documents/cookbook/ingredients/schema.py", line 33, in <module>
class Query(AbstractType):
File "/Users/lebeier/Documents/cookbook/ingredients/schema.py", line 38, in Query
all_ingredients = DjangoFilterConnectionField(IngredientNode)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/graphene_django/filter/fields.py", line 20, in __init__
self.filterset_class = get_filterset_class(filterset_class, **meta)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/graphene_django/filter/utils.py", line 32, in get_filterset_class
return custom_filterset_factory(**meta)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/graphene_django/filter/filterset.py", line 127, in custom_filterset_factory
'Meta': meta_class
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/graphene_django/filter/filterset.py", line 55, in __new__
new_class = super(GrapheneFilterSetMetaclass, cls).__new__(cls, name, bases, attrs)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django_filters/filterset.py", line 224, in __new__
filters[order_by_field] = new_class.get_ordering_filter(opts, filters)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django_filters/filterset.py", line 375, in get_ordering_filter
"'order_by' is not compatible with the 'fields' dict syntax. Use OrderingFilter instead."
AssertionError: 'order_by' is not compatible with the 'fields' dict syntax. Use OrderingFilter instead.
让我大胆的错误Asse田的最后一行:“ORDER_BY”不是与'fields'字典句法兼容。改用OrderingFilter。
在我的成分/schema.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-filter中的某些东西已经改变,导致不兼容。在python2.7中,django-filter版本是0.11.0。有谁知道如何使它在0.15.3工作?
尝试过并没有工作 – Mox
哦,等待这个问题最近才被关闭......我会再试一次 – Mox
不,看起来问题是filter_fields不能再包含字典,并且该示例在RecipeIngredientNode和IngredientNode中使用了字典。您可以使用列表替换字典以符合django过滤器的用法:https://django-filter.readthedocs.io/en/latest/usage.html#the-filter –