【问题标题】:How to update the model with the foreignkey relation in Graphene-Django Relay?如何使用 Graphene-Django Relay 中的外键关系更新模型?
【发布时间】:2020-05-20 05:46:18
【问题描述】:

我正在尝试创建具有 外键关系。我已经按照文档完成了所有操作,但是当我提供外部模型输入时它仍然无法正常工作。

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

【问题讨论】:

    标签: python graphql relay mutation graphene-django


    【解决方案1】:

    只需在突变查询中发送外键的主键即可。而不是

    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
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2020-05-19
      • 2019-12-20
      • 2020-04-20
      • 2018-01-10
      • 2015-10-11
      • 2019-04-22
      • 2013-05-27
      • 2023-01-26
      • 2021-10-11
      相关资源
      最近更新 更多