【发布时间】:2021-05-06 07:48:46
【问题描述】:
我一直在阅读the official documentacion of django 1.11 about bulk updating。我的问题是我只看到了如何更新所有查询对象的字段。我需要的是批量更新,但值不同,具体取决于条件。
让我写一个例子。假设我有以下模型:
class A(models.Model):
key = models.ForeignKey(SomeOtherModel, db_index=True)
value = models.FloatField(blank=True, null=True, default=None)
如您所见,field_definition 指向另一个模型。我想要的是根据该值批量更新模型 A 的查询对象。像这样的:
A.objects.filter(key__id__in=[1,2,3]).update(value_for_id_1="1", ..., value_for_id_n="n")
我唯一看到的就是这种语法:
A.objects.filter(key__id__in=[1,2,3]).update(value="1")
知道这是否可能吗?我怎样才能使这个查询高效?
【问题讨论】:
标签: django django-models