GraphQL 食谱示例中的 Django 过滤器错误

Posted

技术标签:

【中文标题】GraphQL 食谱示例中的 Django 过滤器错误【英文标题】:Django filter error in GraphQL cookbook example 【发布时间】:2017-03-19 00:00:12 【问题描述】:

我目前正在通过有关石墨烯的教程学习 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.

在我的成分/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 中工作?

【问题讨论】:

【参考方案1】:

这是 github 上报告的一个问题,现已修复:https://github.com/graphql-python/graphene-django/issues/8

升级到石墨烯 1.1,一切顺利。

【讨论】:

哦等等这个问题最近才关闭...我会再试一次 不,问题似乎是 filter_fields 不能再包含字典,并且该示例在 RecipeIngredientNode 和成分节点中使用了字典。您可以用列表替换 dicts 以符合 django 过滤器的使用:django-filter.readthedocs.io/en/latest/usage.html#the-filter 以前的文档链接现在已损坏。新链接:django-filter.readthedocs.io/en/develop/guide/… 反正graphene 1.1 与django filter 1.0.0 不兼容,我已经在graphene-python 上报告过了。但它适用于 0.15.3【参考方案2】:

尝试安装 django-filter。

pip install django-filter

【讨论】:

以上是关于GraphQL 食谱示例中的 Django 过滤器错误的主要内容,如果未能解决你的问题,请参考以下文章

如何使用`django-filters`编写将在整数字段上使用范围过滤器的GraphQL查询?

嵌套查询 GraphQl

GraphQL 在 Django 中的一个请求中进行多个查询

当过滤字符串为空时,如何停止 Graphql + Django-Filters 返回所有对象?

如何对 Hasura 中的 ARRAY 字段类型运行 GraphQL 过滤器查询?

如何访问 django 中的链接表