Django + MySQL:同一表列的循环更新

时间:2015-03-12 09:35:22

标签: python mysql django orm

我的模型有点像

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代表上个月,依此类推。因此,如果当前月份是6月,那么

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并将值设置为Move Forward。

如何使用单update()语句实现这一目标?

1 个答案:

答案 0 :(得分:1)

使用F表达式:

from django.db.models import F

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