Update multiple objects at once in Django?

前端 未结 2 424
耶瑟儿~
耶瑟儿~ 2020-12-25 13:07

I am using Django 1.9. I have a Django table that represents the value of a particular measure, by organisation by month, with raw values and percentiles:

cl         


        
2条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-25 13:15

    Atomic transactions can reduce the time spent in the loop:

    from django.db import transaction
    
    with transaction.atomic():
        for i, row in df.iterrows():
            mv = MeasureValue.objects.get(org=row.org, month=month)
    
            if (row.percentile is None) or np.isnan(row.percentile): 
                # if it's already None, why set it to None?
                row.percentile = None
    
            mv.percentile = row.percentile
            mv.save()
    

    Django’s default behavior is to run in autocommit mode. Each query is immediately committed to the database, unless a transaction is actives.

    By using with transaction.atomic() all the inserts are grouped into a single transaction. The time needed to commit the transaction is amortized over all the enclosed insert statements and so the time per insert statement is greatly reduced.

提交回复
热议问题