[英]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.