Django“ bulk_save”和“ bulk_update”

问题描述 投票:6回答:2

更新:增加赏金。请提供示例,我将接受最佳答案

UPDATE 2:现在包括显式示例

Carrying on from the same project, where I asked about bulk_create in a separate thread.我想知道是否存在一种方法来“批量保存”-如果不存在则插入,或者如果已经存在则进行更新。

例如:

class Person(models.Model):
    first_name = models.CharField(max_length=30)
    last_name = models.CharField(max_length=30)
    height = models.DecimalField(blank=True, null=True)
    weight = models.DecimalField(blank=True, null=True)

我有这些字段的键值对字典列表。我想按名称过滤,然后更新身高和/或体重,因为我的球员还在成长和适应中。如果没有简便的方法来“批量保存”,则批量更新也将很有帮助。

Reference: June 8, 2012 - "get_or_create()" patch at django project

Bulk_update reference

database django django-orm
2个回答
3
投票

我只是做了下面列出的update_many函数的一种变体,我似乎已经大大提高了速度。

http://people.iola.dk/olau/python/bulkops.py

更新-显然DSE2也是一个选择。

https://bitbucket.org/weholt/dse2

明天将更新速度测试。


0
投票

如果您正在谈论Django表单集,这可能会帮助https://docs.djangoproject.com/en/3.0/topics/forms/formsets/

© www.soinside.com 2019 - 2024. All rights reserved.