有谁知道是否可以在单个查询中同时执行 .select_for_update() 和 .select_lated() 语句?例如:
employee = get_object_or_404(Employee.objects.select_for_update().
select_related(‘company’), pk=3)
它似乎在我的代码中的一个地方工作正常,但第二次使用引发了一系列单元测试的“内部错误:当前事务已中止”。删除 .select_lated 并仅保留 .select_for_update 使错误消失,但我不知道为什么。我想使用它们来优化我的代码,但如果被迫选择,我会选择 select_for_update。想知道是否有办法可以同时使用两者。使用 postgres 和 django 1.9。谢谢!
从 Django 2.0 开始,你can use select_for_update
和...一起select_related
即使在可空关系上 - 通过使用新参数of=...
使用他们的Person
文档中的示例,你可以这样做
Person.objects.select_related('hometown').select_for_update(of=('self',))
这只会锁定Person
object
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)