【发布时间】:2021-02-07 13:59:31
【问题描述】:
我有一个带有 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?谢谢
【问题讨论】:
标签: django graphql django-filter relay graphene-django