Hi everyone!
I’m currently developing django-bulk-save, a Python package born from a performance issue I encountered while working on a large Django codebase.
The goal is to make bulk persistence easier when Django models require custom pre-save logic.
In Django, bulk_update() and bulk_create() are great for reducing database queries, but they bypass custom save() implementations. This can become problematic when models rely on additional logic before being persisted.
In my case, I ended up calling .save() inside a for loop in several situations, resulting in individual database operations that I wanted to avoid.
django-bulk-save aims to address this by introducing a BulkSaveableModel that provides:
- A customizable
before_save()hook for model-specific logic. - A context manager for registering instances and managing the bulk persistence lifecycle.
- Automatic handling of bulk creates and updates within a database transaction.
- PostgreSQL-optimized bulk operations powered by pgbulk.
The idea is to keep the API simple while allowing developers to perform bulk operations without sacrificing custom pre-save logic.
The package is still in its early stages.
GitHub: https://github.com/lorst97/django-bulk-save
Contributions and suggestions are very welcome!