我的模特有点像

Spot
    spot_1 = 1/0
    spot_2 = 1/0
    spot_3 = 1/0
    spot_4 = 1/0
    spot_5 = 1/0
    spot_6 = 1/0

类似于循环数据库,假设spot_1表示当前月份,spot_2表示上个月,依此类推。如果这个月是六月,那么
Jun  = spot_1
May  = spot_2
Apr  = spot_3
Mar  = spot_4
Feb  = spot_5
Jan  = spot_6

因此,在6月底7月初,这将表现为
July = spot_1
Jun  = spot_2
May  = spot_3
Apr  = spot_4
Mar  = spot_5
Feb  = spot_6

因此,这些价值观也应该得到弘扬。目前,我正在遍历完整的QuerySet并使值向前移动。
如何通过单个update()语句实现这一点?

最佳答案

使用F表达式:

from django.db.models import F

Spot.objects.all().update(spot_2=F('spot_1'), spot_3=F('spot_2'), ...)

09-25 21:31