【发布时间】:2014-04-14 20:06:29
【问题描述】:
我有下表“书”:
id shelf position (on shelf)
1 2 3
2 1 1
3 1 2
4 2 2
5 2 1
我需要将 book4 移动到书架 1 的末尾,因此我需要设置 book4 位置到 [max(position) whereshelf=1] + 1 和位置 where position=[max(position) where Shelf=2] 到 book4 的初始位置(缩小差距)
我试过这个,但 Max 似乎不是这样工作的......
Book.objects.filter(shelf=2).annotate(max_position_on_shelf=Max('position'))\
.filter(position=F('max_position_on_shelf'))\
.update(position=F('max_position_on_shelf') + 1)
【问题讨论】:
标签: django django-orm