在一对一字段键上更新未给我完整性错误:
模型看起来像:
store = models.ForeignKey(Store, on_delete=models.CASCADE, db_index=True)
credit_type = models.OneToOneField(CreditType, on_delete=models.CASCADE)
value = models.IntegerField(validators=[MinValueValidator(0)], default=0)
api看起来像:
curd = 'add'
operate = {'add': operator.add, 'delete': operator.sub}
try:
for index, c_id in enumerate(credit_ids):
StoreCredits.objects.filter(store_id=store_id, credit_type_id=c_id)\
.update(value=operate[curd](F('value'), credit_values[index]))
except IntegrityError:
return {"wrong key entered try again."}
return {"success"}
其他信息:api函数有:@transaction.atomic
,amn不能将POST值转换为int,尽管数据库有int类型,
只需添加或删除值就可以正常工作,但我想处理错误的ID。对于外键当传递错误的id时,它抛出完整性错误,在这里它不抛出错误,只返回'success'
它只是将值添加到匹配的credit\u id中,而不与其他id进行任何操作
目前没有回答
相关问题 更多 >
编程相关推荐