DjangoObjectType的graphene-django自定义get_queryset不起作用

时间:2019-07-08 12:35:49

标签: graphene-django

在官方文档中:https://docs.graphene-python.org/projects/django/en/latest/queries/#default-queryset。实际上,没有任何反应。请帮忙。例如,在下面,我仅具有模型Contact的简单代码部分,其中包含3个字段(id,kind,value)。我希望ContactType始终使用kind ='PHONE'进行过滤。

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"
      }
    ]
  }
}

1 个答案:

答案 0 :(得分:0)

对于寻求该问题答案的任何人,其答案均为here

尝试将列表替换为DjangoConnectionField。 看起来我们需要证明get_queryset与DjangoConnectionField一起使用,并且非中继字段(如List)没有相同的魔力。