我是石墨烯库的新手,并按照文档中的方式进行更新突变。 我还看了这个关于 CRUD 的视频:https://youtu.be/C-EfYVXShLE?si=uksgjJEuavU1k9GW 我想,这个问题出在变异参数中,我尝试用 kwargs 来解决,但结果是一样的。不幸的是,大多数指南都使用 django-rest 来编写 CRUD,但我想在没有其他依赖的情况下实现它。
这是代码:
class UpdateAddress(graphene.Mutation):
address = graphene.Field(AddressType)
class Arguments:
address_id = graphene.ID(required=True)
index = graphene.String()
street = graphene.String()
house = graphene.String()
flat = graphene.String()
locality = graphene.Int()
@classmethod
def mutate(cls, root, info, address_id, index=None, street=None, house=None, flat=None, locality=None):
address = Address.objects.get(pk=address_id)
try:
entry = Locality.objects.get(pk=locality)
except Locality.DoesNotExist:
entry = None
address.index=index,
address.street=street,
address.house=house,
address.flat=flat,
address.locality=entry
address.save()
return UpdateAddress(address=address)
当我尝试使用此查询更新某些字段时:
mutation {
updateAddress(
addressId: "1",
index: "41204"
street: "Shevchenko",
house: "1",
flat: "15",
locality: 1
) {
address{
id,
street,
index,
house,
flat,
locality{
id
}
}
}
}
我收到错误:
"errors": [
{
"message": "String cannot represent value: ('41204',)",
"locations": [
{
"line": 12,
"column": 7
}
],
"path": [
"updateAddress",
"address",
"index"
]
}
],
不知何故,它将所有字符串值设置为一个元组,我得到这样的字段:
"id": "1",
"index": "('41204',)",
"street": "('Shevchenko',)",
"house": "('1',)",
"flat": "('1',)"
老实说,我不明白我做错了什么:(
删除末尾的逗号。您将这些值包装在一个单例元组中:
address.index = index # 🖘 no comma
address.street = street # 🖘 no comma
address.house = house # 🖘 no comma
address.flat = flat # 🖘 no comma
address.locality = entry # 🖘 no comma