0

我面临的问题是我有custom_filter哪个MyModel返回<QuerySet>喜欢的列表

[<QuerySet [<MyModel: xyz>]>, <QuerySet [<MyModel: xyz>, <MyModel: xyz>,<MyModel: xyz>]>]

对象类型

class MyModelNode(DjangoObjectType):
    class Meta:
        model=MyModel
        filter_fields=['id]
        interfaces = (graphene.relay.Node,)


询问

class Query(graphene.ObjectType):
   my_model_items = graphene.List(MyModelNode)

   def resolve_my_model_items(self, info, **kwargs):
      my_model_filtered_items = MyModel.objects.custom_filter(kwargs)
      # my_model_filtered_items holds the list of querysets
      return my_model_filtered_items

如何处理查询集列表。查询的 graphql 响应应该给出一个列表,其中包含查询集作为元素。

[
  {
  //These are from first <QuerySet>
  "myModelItems":[
      {
        "fieldsIaskedFor":"response"
      }
    ]

  },


  {
  //These are from second <QuerySet>
  "myModelItems":[
      {
        "fieldsIaskedFor":"resp"
      },
      {
        "fieldsIaskedFor":"resp"
      },
      {
        "fieldsIaskedFor":"resp"
      },
    ]

  },


]

如何在不同的列表元素中获取不同查询集的结果?数量<QuerySet>不固定。

我必须做些什么来实现这一目标?

4

1 回答 1

1

这可以通过创建两种对象类型并将一个简单地嵌套在另一个中来完成。

第一个对象类型将DjangoObjectTypeMyModel

class DjangoMyModelNode(DjangoObjectType):
    class Meta:
        model = MyModel
        filter_fields = ['id']
        interfaces = (graphene.relay.Node,)

第二种对象类型将是自定义ObjectType

class MyModelNode(graphene.ObjectType):
    my_model_items = graphene.List(DjangoMyModelNode)

    def resolve_my_model_items(self, info, **kwargs):
        return self

查询将保持不变

class Query(graphene.ObjectType):
    my_model_items = graphene.List(MyModelNode)

    def resolve_my_model_items(self, info, **kwargs):
        my_model_filtered_items = MyModel.objects.custom_filter(kwargs)
        # my_model_filtered_items holds the list of querysets
        return my_model_filtered_items

这将根据期望的结果返回东西

于 2019-07-04T13:19:52.443 回答