Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 根据相应字段将列组更新为django模型记录_Python_Django - Fatal编程技术网

Python 根据相应字段将列组更新为django模型记录

Python 根据相应字段将列组更新为django模型记录,python,django,Python,Django,我有一个Django模型,其中记录每周创建一次。每个记录都会计算出每月、1年、3年等的十进制值。此输入数据以查询集的形式提供 然后在列中按记录创建这些记录 输入数据以以下格式作为查询集提供 输入数据 item_code | ason_date | v_monthly | v_yearly_1 |... --------------------------------------------------- 1 | 2020-10-05 | 9.05 | 7

我有一个Django模型,其中记录每周创建一次。每个记录都会计算出每月、1年、3年等的十进制值。此输入数据以查询集的形式提供

然后在列中按记录创建这些记录

输入数据以以下格式作为查询集提供

输入数据

item_code  |  ason_date  | v_monthly | v_yearly_1 |...
---------------------------------------------------
   1       |  2020-10-05 |   9.05    |    7.43    |...
   2       |  2020-10-05 |   7.05    |    9.43    |...
   3       |  2020-10-05 |   4.05    |    2.43    |...
   4       |  2020-10-05 |   6.05    |    5.43    |...
   5       |  2020-10-05 |   1.05    |    8.46    |...
我以降序对查询集进行排序w.r.t.
v_monthly
,然后在我的排名模型中创建记录,如下所示

rank_obj = []
rank = 1
for rec in input_data:
    rank_kwargs = {"item_code" : rec["item_code"],
                   "ason_date" : rec["ason_date"],
                   "v_monthly" : rec["v_monthly"],
                   "r_montlhy" : rank,
                   "v_yearly_1": rec["v_yearly_1"],
                   ... ...
                   }
    rank_obj.append(Ranks(**rank_kwargs))
    rank+=1
Ranks.bulk_create(rank_obj)
我尝试过更新其他等级,但由于记录数量巨大,这非常耗时

all_vals = Ranks.objects.filter(ason_date = datetime(2020,10,5))
cal_vals = ["yearly_1","yearly_2", "yearly_3", .......]

for field in cal_vals:
    value_field = "-v_"+field
    rank_field = "r_"+field
    order = all_vals.order_by(value_field)
    rank = 1
    for rec in order:
        rec.update(rank_field = rank)
        rank+=1
有没有更快的办法?欢迎提出任何意见。非常感谢

预期产量

item_code  |  ason_date  | v_monthly | r_monthly | v_yearly_1 | r_yearly_1 |...
----------------------------------------------------------------------------
   1       |  2020-10-05 |   9.05    |     1     |    7.43    |     3      |... 
   2       |  2020-10-05 |   7.05    |     2     |    9.43    |     1      |... 
   3       |  2020-10-05 |   4.05    |     4     |    2.43    |     5      |...
   4       |  2020-10-05 |   6.05    |     3     |    5.43    |     4      |...
   5       |  2020-10-05 |   1.05    |     5     |    8.46    |     2      |...