简体   繁体   English

Graphene-Django嵌套过滤器(中继)

[英]Graphene-Django nested filters (relay)

I'm trying to achieve the following using graphene-django's DjangoFilterConnectionField : 我正在尝试使用graphene-django的DjangoFilterConnectionField实现以下DjangoFilterConnectionField

{
  allUsers(username_Icontains: "abc") {
    edges {
      node {
        id
        demographics (name_Icontains: "xyz") {
          id
          name
        }
      }
    }
  }
}

I know that in graphene django, it's possible to have nested filtering using graphene's List But I'm not sure if I can work this out using DjangoFilterConnectionField . 我知道在graphene django中,可以使用graphene的List进行嵌套过滤,但是我不确定是否可以使用DjangoFilterConnectionField解决此问题。

I have the following graphene (relay) schema: 我有以下石墨烯(中继)架构:

class UserNode(DjangoObjectType):
    class Meta:
        model = User
        interfaces = (Node,)
        filter_fields = {
            'username': ['exact', 'icontains', 'in'],
        }

class DemographicNode(DjangoObjectType):
    class Meta:
        model = Demographic
        interfaces = (Node,)
        filter_fields = {
            'name': ['icontains'],
        }

 class Query(ObjectType):

    user = Node.Field(UserNode)
    all_users = DjangoFilterConnectionField(UserNode)

    demographic = Node.Field(DemographicNode)
    all_demographics = DjangoFilterConnectionField(DemographicNode)

In the docs it's suggested to introduce each filter on the connected node as well. 文档中 ,建议在连接的节点上也引入每个过滤器。 So it would be like this: 所以会像这样:

class UserNode(DjangoObjectType):
    class Meta:
        model = User
        interfaces = (Node,)
        filter_fields = {
            'username': ['exact', 'icontains', 'in'],
            'demographic__name': ['icontains']
        }

But I think there must be a better way to do this since I have to do this for more than 20 nested Nodes. 但是我认为必须有更好的方法来执行此操作,因为我必须对20个以上的嵌套节点执行此操作。

I think you have OneToOneField between this two model, if it's true, you can't have something like that because it's not logical 我认为您在这两个模型之间有一个OneToOneField,如果的确是这样,则不能有类似的内容,因为它不合逻辑

imagine that, if a user has a Demographic, and you retrieve allUsers, then for each one, we have one Demographic and it's not logical to filter one Demographic. 想象一下,如果一个用户有一个人口统计信息,而您检索了allUsers,那么对于每个用户,我们都有一个人口统计信息,并且过滤一个人口统计信息是不合逻辑的。 the only way to do this is to filter it with the UserNode(as you said and did) 做到这一点的唯一方法是使用UserNode对其进行过滤(如您所说和所做的)

the Nested filtering just work if you have a ForeignKey or ManyToManyField to user model and then you can retrieve that in user with the separate filter 如果您要对用户模型使用ForeignKey或ManyToManyField,则嵌套过滤就可以工作,然后可以使用单独的过滤器在用户中检索

I hope I've been able to make it clear to you 希望我能向您说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM