[英]Django-Graphene: On a model ChoiceField, graphene expects a Type but got a value
On Django-Graphene, I have this model:在 Django-Graphene 上,我有这个 model:
class Entry(models.Model):
STATE_CHOICES = [
("Open", "Open"),
("Processing", "Processing"),
("Closed", "Closed"),
("Deleted", "Deleted"),
]
# ...
state = models.CharField(max_length=10, choices=STATE_CHOICES,
default="Open")
With the following Graphene schema:使用以下石墨烯模式:
class EntryType(DjangoObjectType):
class Meta:
model = models.Entry
class Query(graphene.ObjectType):
entries = graphene.List(EntryType)
def resolve_entries(self, info):
return models.Entry.objects.all()
But when I use the next query:但是当我使用下一个查询时:
query AllEntries{
entries{
id
state
}
}
I get this error:我收到此错误:
{
"errors": [
{
"message": "Expected a value of type \"EntryState\" but received: OPEN",
"path": [
"entries",
1,
"state"
]
}
],
}
Can someone explain me what I'm doing wrong?有人可以解释我做错了什么吗?
This is because of this line:这是因为这一行:
state = models.CharField(max_length=10, choices=STATE_CHOICES, default="Open")
Even though this is accepted by the ORM and is saved correctly in the database, it confuses graphene because it's trying to compare a string with an enum value.即使这被 ORM 接受并正确保存在数据库中,它也会混淆石墨烯,因为它试图将字符串与枚举值进行比较。
To remedy this, you can do it like this:为了解决这个问题,你可以这样做:
state = models.CharField(max_length=10, choices=STATE_CHOICES, default=STATE_CHOICES.Open)
If you do not want to create/do migration, you can alternatively create a graphene ENUM type then map it inside your resolve_state
function.如果您不想创建/执行迁移,您也可以在您的
resolve_state
function 中创建一个石墨烯 ENUM 类型,然后创建 map。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.