Skip to content

Latest commit

 

History

History
66 lines (51 loc) · 2.11 KB

README.md

File metadata and controls

66 lines (51 loc) · 2.11 KB

Django Bulk Copy 🚀

License: MIT

PyPI Python

Poetry Black Mypy isort bandit

Install 🛠️

⚠️ It only supports PostgreSQL for now.

pip install 'django-bulk-copy[postgres]'

Usage 🚀

# models.py
from bulk_copy import BulkCopyManager
from django.db import models

class DummyModel(models.Model):
    integer_field = models.IntegerField(null=True)
    char_field = models.CharField(max_length=32, null=True)
    boolean_field = models.BooleanField(default=False)
    datetime_field = models.DateTimeField(null=True)
    json_field = models.JSONField(null=True)

    objects = BulkCopyManager()

# Usage
objects = [
    DummyModel(
        integer_field=i,
        char_field=str(i),
        boolean_field=bool(i % 2),
        datetime_field=timezone.now(),
        json_field={i: f"{i:>09}"},
    )
    for i in range(1000)
]

TestModel.objects.bulk_copy(objects)

If your model has a date/datetime field with auto_now=True, bulk_copy will use the transaction's initial time instead of the object's creation.

Benchmark 📊

Object Count bulk_create bulk_copy
1.000 0.06 0.05
10.000 0.34 0.08
100.000 3.96 0.80
1.000.000 38.96 7.57

Additional Note 📝

If you need to create your models from a csv file, django-postgres-copy could be a better alternative.