[英]How to update the model with the foreignkey relation in Graphene-Django Relay?
我正在尝试创建具有外键关系的模型的 Update 突变。 我已经按照文档完成了所有操作,但是当我提供外部模型输入时它仍然不起作用。
我使用我应该在查询中传递的正确属性创建了输入,但它不起作用并抛出下面显示的错误。 字段 'id' 需要一个数字,但得到了 {'id': 2}。
我无法理解此错误背后的原因。 我正在传递正确的输入(我相信)有人可以帮助我理解为什么会这样吗?
非常感谢您的建议和意见。
输入:
class FuelTypeInput(graphene.InputObjectType):
# id of the FuelTypeModel
id = graphene.Int()
label = graphene.String()
class FuelSubtypeInput(graphene.InputObjectType):
# Graphene ID
id = graphene.ID()
label = graphene.String()
fuel_type = graphene.Field(FuelTypeInput)
更新突变:
class UpdateFuelSubType(relay.ClientIDMutation):
class Input:
id = Int() # id of the Fuel SubTypeModel
input = FuelSubtypeInput(required=True)
ok = True
fuel_subtype = Field(FuelSubTypeNode)
def mutate_and_get_payload(root, info, id, input):
ok = False
if FuelSubType.objects.filter(pk=id).update(**input):
fuel_subtype = FuelSubType.objects.get(pk=id)
ok = True
return UpdateFuelSubType(fuel_subtype=fuel_subtype)
return UpdateFuelSubType(fuel_subtype=None)
客户端的变异查询:
mutation MyMutations {
updateFuelSubtype(
input: {
id: 2,
input: { label: "Updated 11 Mutation Label",
fuelType: { id: 2 }
}
}
) {
fuelSubtype {
label
}
}
}
最后结果:
{
"errors": [
{
"message": "Field 'id' expected a number but got {'id': 2}.",
"locations": [
{
"line": 48,
"column": 5
}
],
"path": [
"updateFuelSubtype"
]
}
],
"data": {
"updateFuelSubtype": null
}
}
我还想提一下,当我从查询中删除fuelType
输入时,一切正常,例如:
mutation MyMutations {
updateFuelSubtype(
input: {
id: 2,
input: { label: "Updated 11 Mutation Label" }
}
) {
fuelSubtype {
label
}
}
}
只需在变异查询中发送外键的主键即可。 代替
mutation MyMutations {
updateFuelSubtype(
input: {
id: 2,
input: { label: "Updated 11 Mutation Label",
fuelType: { id: 2 }
}
}
) {
fuelSubtype {
label
}
}
}
发送
mutation MyMutations {
updateFuelSubtype(
input: {
id: 2,
input: { label: "Updated 11 Mutation Label",
fuelType: 2 # ## <--- change
}
}
) {
fuelSubtype {
label
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.