[英]How to use MultipleChoiceFilter in graphene-django?
我有一个带有 graphql 端点的 Django 应用程序。 我需要能够通过某个字段的多个值一次过滤对象。
我有以下石墨烯方案:
class ChannelFilter(FilterSet):
type = MultipleChoiceFilter(choices=Channel.TYPES)
class Meta:
model = Channel
fields = ['type']
class ChannelNode(DjangoObjectType):
class Meta:
model = Channel
filter_fields = ['type']
interfaces = (relay.Node,)
class Query(graphene.ObjectType):
channels = DjangoFilterConnectionField(
ChannelNode, filterset_class=ChannelFilter
)
schema = graphene.Schema(query=Query)
然后我尝试了以下 graphql 查询来过滤我的对象:
query {
channels(type: "BOT") {
edges {
node {
id
}
}
}
}
结果,出现以下错误:
{
"errors": [
{
"message": "['{\"type\": [{\"message\": \"Enter a list of values.\", \"code\": \"invalid_list\"}]}']",
"locations": [
{
"line": 2,
"column": 3
}
],
"path": [
"channels"
]
}
],
"data": {
"channels": null
}
}
query {
channels(type: ["BOT"]) {
edges {
node {
id
}
}
}
}
结果,出现以下错误:
{
"errors": [
{
"message": "Argument \"type\" has invalid value [\"BOT\"].\nExpected type \"String\", found [\"BOT\"].",
"locations": [
{
"line": 2,
"column": 18
}
]
}
]
}
如何正确使用 MultipleChoiceFilter? 谢谢
您可能需要将表单域转换器注册为
import graphene
from graphene_django.forms.converter import convert_form_field
from django_filters.fields import MultipleChoiceField
@convert_form_field.register(MultipleChoiceField)
def convert_multiple_choice_filter_to_list_field(field):
return graphene.List(graphene.String, required=field.required)
然后使用channels(type: ["BOT"])
符号进行过滤。
您可以将注册代码片段放在任何地方,但是,请确保它在服务器启动时执行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.