GraphQL cookbook 示例中的 Django 过滤器错误
Django filter error in GraphQL cookbook example
我目前正在通过石墨烯教程学习 GraphQL,但是当我尝试 运行 时收到此错误消息:
python3 manage.py graphql_schema
错误:
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.
在我的 ingredient/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-filter 版本 0.15.3
我尝试从官方石墨烯存储库中克隆示例,它在 python2.7 上运行良好。但是在 python3.5 中,相同的代码无法工作。我怀疑 django-filter 中的某些内容发生了变化,导致不兼容。 python2.7中,django-filter版本为0.11.0。有谁知道如何让它在 0.15.3 中工作?
这是 github 上的一个问题,现已修复:https://github.com/graphql-python/graphene-django/issues/8
升级到石墨烯 1.1,你应该可以开始了。
尝试安装 django-filter。
pip install django-filter
我目前正在通过石墨烯教程学习 GraphQL,但是当我尝试 运行 时收到此错误消息:
python3 manage.py graphql_schema
错误:
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.
在我的 ingredient/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-filter 版本 0.15.3
我尝试从官方石墨烯存储库中克隆示例,它在 python2.7 上运行良好。但是在 python3.5 中,相同的代码无法工作。我怀疑 django-filter 中的某些内容发生了变化,导致不兼容。 python2.7中,django-filter版本为0.11.0。有谁知道如何让它在 0.15.3 中工作?
这是 github 上的一个问题,现已修复:https://github.com/graphql-python/graphene-django/issues/8
升级到石墨烯 1.1,你应该可以开始了。
尝试安装 django-filter。
pip install django-filter