如何使用grapheneDjango和Django过滤器在Django中创建“不相等”和“不在”过滤器

2024-09-24 00:31:39 发布

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

我试图找到如何执行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 equalnot in一样

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

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

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


Tags: inpyobjectmodelsnotequalresult解决方案