如何在石墨烯-姜戈中继中使用外键关系更新模型?



我正在尝试创建具有以下特征的模型的更新突变 外键关系。我已经按照文档完成了所有操作,但是当我提供外部模型输入时它仍然不起作用。

我创建了具有应在查询中传递的正确属性的输入 但它不起作用并抛出下面显示的错误。字段"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
}
}
}

最新更新