Lifecycle-style hooks for Django bulk operations like bulk_create and bulk_update.
Project description
django-bulk-hooks
⚡ Salesforce-style hooks hooks for Django bulk operations.
django-bulk-hooks brings a declarative, trigger-like experience to Django's bulk_create, bulk_update, and bulk_delete — including support for BEFORE_ and AFTER_ hooks, conditions, batching, and transactional safety.
✨ Features
- Declarative hook system:
@hook(AFTER_UPDATE, condition=...) - BEFORE/AFTER hooks for create, update, delete
- Salesforce-style semantics with full batch support
- Lifecycle-aware manager that wraps Django’s
bulk_operations - Hook chaining, trigger deduplication, and atomicity
- Class-based hook handlers with DI support
🚀 Quickstart
pip install django-bulk-hooks
Define Your Model
from django.db import models
from django_bulk_hooks.manager import BulkLifecycleManager
class Account(models.Model):
balance = models.DecimalField(max_digits=10, decimal_places=2)
objects = BulkLifecycleManager()
Create a Trigger Handler
from django_bulk_hooks import hook, AFTER_UPDATE, TriggerHandler
from django_bulk_hooks.conditions import WhenFieldHasChanged
from .models import Account
class AccountTriggerHandler(TriggerHandler):
@hook(AFTER_UPDATE, model=Account, condition=WhenFieldHasChanged("balance"))
def log_balance_change(self, new_objs):
print("Accounts updated:", [a.pk for a in new_objs])
🛠 Supported Lifecycle Events
BEFORE_CREATE,AFTER_CREATEBEFORE_UPDATE,AFTER_UPDATEBEFORE_DELETE,AFTER_DELETE
🧠 Why?
Django’s bulk_ methods bypass signals and save(). This package fills that gap with:
- Triggers that behave consistently across creates/updates/deletes
- Scalable performance via chunking (default 200)
- Support for
@hookdecorators and centralized trigger classes
📦 Usage in Views / Commands
# Calls AFTER_UPDATE hooks automatically
Account.objects.bulk_update(accounts, ['balance'])
# Triggers BEFORE_CREATE and AFTER_CREATE
Account.objects.bulk_create(accounts)
🧩 Integration with Queryable Properties
You can extend from BulkLifecycleManager to support formula fields or property querying.
class MyManager(BulkLifecycleManager, QueryablePropertiesManager):
pass
📝 License
MIT © 2024 Augend / Konrad Beck
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file django_bulk_hooks-0.1.51.tar.gz.
File metadata
- Download URL: django_bulk_hooks-0.1.51.tar.gz
- Upload date:
- Size: 8.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.1.3 CPython/3.12.10 Windows/11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ec312ae2450bfff38a55d0bf8f17f77deb53f03b228a0e09ed70d8c9861d54b8
|
|
| MD5 |
b94d385b6b0557c35b4540b292bd2329
|
|
| BLAKE2b-256 |
5a033ca8c0204f71c90cf30a2db1f5e453433346d2370d8f7c397c5786aaf8b4
|
File details
Details for the file django_bulk_hooks-0.1.51-py3-none-any.whl.
File metadata
- Download URL: django_bulk_hooks-0.1.51-py3-none-any.whl
- Upload date:
- Size: 12.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.1.3 CPython/3.12.10 Windows/11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
af06fe02b3064b90e5d76e972bd33809e2baf43afe7220fbc0fdfbc1ca2908c1
|
|
| MD5 |
77053d9adc5464620258ae7555a12116
|
|
| BLAKE2b-256 |
ae709ea8f01c4a4087c47b23f64275fe817d89c58239c33309d4f2211d2b3fb5
|