Skip to content

angryfoxx/django-bulk-copy

 
 

Repository files navigation

Django Bulk Copy 🚀

License: MIT pre-commit.ci status GitHub contributors

PyPI PyPI - Python Version PyPI - Downloads PyPI - Format PyPI - Status PyPI - Implementation PyPI - Wheel

Poetry Black Mypy isort bandit

Create your mass data in a faster way with the COPY command.

⚠️ Currently it supports only the Postgresql database. ⚠️

Install 🛠️

pip install django-bulk-copy

Usage 🚀

class TestModel(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)
from bulk_copy import BulkCopy

objects = [
    TestModel(
        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)
]

BulkCopy(objects)

BulkCopy only uses the initial time of the transaction if your model has a date/datetime field with auto_now=True.

Benchmark 📊

Object Count bulk_create BulkCopy
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.

About

No description, website, or topics provided.

Resources

License

Code of conduct

Security policy

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Python 96.6%
  • Shell 3.4%