BulkProcessMixin on models.Model
— at Version 5
New Feature for convinient context manager for bulk create/update.
Just inherit MoModelBulkProcessMixin on Model class.
class BulkyModel(models.Model, ModelBulkProcessMixin):
id = models.BigAutoField(primary_key=True)
name = models.CharField(max_length=10, null=False)
names = [f"name-{num}" for num in range(100_100_000)]
# Case : Raw bulk_create
objs = [BulkyModel(name=name) for name in names]
BulkyModel.objects.bulk_create(
objs
) # We should maintain big array size and this leades to OOM error
# Case : Chunked bulk_create
objs = list()
for name in names:
obj = BulkyModel(name=name)
objs.append(obj)
if len(objs) > 10_1000:
BulkyModel.objects.bulk_create(objs)
objs.clear()
if len(objs) > 0:
BulkyModel.objects.bulk_create(objs)
objs.clear()
# Case : With ModelBulkProcessMixin
with BulkyModel.gen_bulk_create(batch_size=10_000) as bulk:
for name in names:
bulk.add(BulkyModel(name=name))
https://github.com/django/django/pull/15578
Change History
(5)
Owner: |
changed from nobody to Myung Eui Yoon
|
Status: |
new → assigned
|
Description: |
modified (diff)
|
Has patch: |
set
|
Description: |
modified (diff)
|
Description: |
modified (diff)
|
Description: |
modified (diff)
|
Issued Pull Request
https://github.com/django/django/pull/15577