在官方文档中: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"
}
]
}
}