首页 > 解决方案 > 如何使用 graphene-django 和 django-filters 在 django 中创建“不等于”和“不在”过滤器

问题描述

我试图找到有关如何执行not equalnot in过滤的解决方案,但由于我的经验不足,我无法确定解决方案。

这是我当前结构的一个片段。

# result/models.py
class Result(models.Model):
    ...

    object = models.ForeignKey(Object, on_delete=models.PROTECT)
    ...
# result/types.py
class ResultType(DjangoObjectType):
    class Meta:
        model = Result
        filter_fields = {
            ...
            'object__object_name': ['exact', 'icontains'],
            ...
        }

        convert_choices_to_enum = False
        interfaces = (graphene.relay.Node,)

        object = DjangoFilterConnectionField(ObjectType)
# result/queries.py
class Query(object):
    results = DjangoFilterConnectionField(ResultType)

我希望能够执行类似于以下的搜索:

# result/types.py
class ResultType(DjangoObjectType):
    class Meta:
        model = Result
        filter_fields = {
            ...
            'object__object_name': ['exact', 'icontains', 'notequal', 'notin'],
            ...
        }
        convert_choices_to_enum = False
        interfaces = (graphene.relay.Node,)
        object = DjangoFilterConnectionField(ObjectType)

因此,您可以执行下面的查询,就像not equal使用not in.

query {
  results ( object_ObjectName__Notequal: "Pluto"){
    edges {
      node {
        object {
          objectName
        }
      }
    }
  }
}

更新(2021-03-18):已解决

用户tcleonard向我展示了达到此结果的方法。 https://github.com/graphql-python/graphene-django/issues/1145

标签: pythondjangographqlgraphene-pythondjango-filters

解决方案


推荐阅读