1

在官方文档中:https ://docs.graphene-python.org/projects/django/en/latest/queries/#default-queryset 。在实践中什么都没有发生。请帮忙。下面,例如,我只有简单的部分代码,模型联系人有 3 个字段(id、种类、值)。我希望始终使用 kind='PHONE' 过滤 ContactType。

class ContactType(DjangoObjectType):
    class Meta:
        model = Contact

    @classmethod
    def get_queryset(cls, queryset, info):
        return queryset.filter(kind='PHONE')

class ContactQuery(graphene.ObjectType):
    contacts = graphene.List(ContactType)

    def resolve_contacts(self, info, **kwargs):
        return Contact.objects.all()

查询:

{
    contacts {
        id
        kind
        value
    }
}

返回:

{
  "data": {
    "contacts": [
      {
        "id": "1",
        "kind": "INSTAGRAM",
        "value": "technodomkz"
      },
      {
        "id": "3",
        "kind": "YOUTUBE",
        "value": "UCF-HjvMMvgnhXhO4shk9i9Q"
      },
      {
        "id": "4",
        "kind": "FACEBOOK",
        "value": "technodomkz"
      },
      {
        "id": "5",
        "kind": "TWITTER",
        "value": "technodom_kz"
      },
      {
        "id": "6",
        "kind": "PHONE",
        "value": "88000801111"
      },
      {
        "id": "7",
        "kind": "PHONE",
        "value": "87272799999"
      },
      {
        "id": "8",
        "kind": "EMAIL",
        "value": "callcenter@technodom.kz"
      },
      {
        "id": "2",
        "kind": "WEBSITE",
        "value": "www.technodom.kz"
      }
    ]
  }
}

代替:

{
  "data": {
    "contacts": [
      {
        "id": "6",
        "kind": "PHONE",
        "value": "88000801111"
      },
      {
        "id": "7",
        "kind": "PHONE",
        "value": "87272799999"
      }
    ]
  }
}
4

1 回答 1

0

对于任何寻找这个问题的答案的人,在这里得到了回答

尝试将 List 换成 DjangoConnectionField。看起来我们需要记录 get_queryset 与 DjangoConnectionField 一起使用,并且像 List 这样的非中继字段没有相同的魔力。

于 2020-06-16T19:12:02.957 回答