A next-generation, high-performance declarative lifecycle hooks library for Django 5.x+
Project description
Django Lifecycle Hooks
The next-generation, high-performance declarative lifecycle hooks library for Django.
Engineered for Enterprise projects requiring surgical precision, zero runtime overhead, and full compatibility with the modern Python stack (3.10 through 3.14+) and Django 4.x+.
🚀 Why this library?
Legacy lifecycle libraries rely on heavy runtime introspection and full object copying, causing significant memory bloat and CPU drag. Django Lifecycle Hooks changes the game:
- ⚡ Zero-Overhead Runtime: The hook registry is pre-computed once at import time. Executing hooks during a request is an O(1) lookup operation.
- 🧠 Intelligent Memory Management: Uses Sparse Snapshotting—we only track fields that are actually watched. If you have a model with 50 fields but only watch
status, we only cachestatus. - 💎 Python 3.14+ & Django 5.2+ Ready: Built strictly with modern typing (
Self,|unions),__slots__optimizations, and nativetransaction.on_commitsupport. - 🛡️ Type Safe: 100% typed codebase ready for strict MyPy validation.
Installation
pip install django-lifecycle-hooks
Usage
from django.db import models
from django_lifecycle_hooks import LifecycleModelMixin, hook, HookType
class UserAccount(LifecycleModelMixin, models.Model):
username = models.CharField(max_length=100)
status = models.CharField(max_length=20, default="active")
email_sent = models.BooleanField(default=False)
login_count = models.IntegerField(default=0)
# 1. Simple trigger: Run logic before saving
@hook(HookType.BEFORE_SAVE)
def clean_username(self):
self.username = self.username.lower()
# 2. Conditional trigger: Run only when status changes to 'banned'
@hook(HookType.BEFORE_UPDATE, when="status", was="active", is_now="banned")
def on_ban(self):
print(f"Banning user {self.username}")
self.email_sent = False
# No need to call save(), we are in BEFORE_UPDATE
# 3. Transaction aware: Run only after DB commit succeeds
@hook(HookType.AFTER_SAVE, when="status", has_changed=True, on_commit=True)
def notify_external_system(self):
# Safe to launch async tasks or external API calls here
print(f"Syncing status {self.status} to CRM...")
⚡ Performance Architecture
We take performance seriously. Here is how we differ from the rest:
| Feature | Legacy Libraries | Django Lifecycle Hooks |
|---|---|---|
| Hook Resolution | Runtime Introspection (Slow) | Import-time Registry (Instant) |
| Change Detection | Full __dict__ copy (High RAM) |
Sparse Field Copy (Low RAM) |
| Data Structure | Standard Dictionaries | __slots__ Optimized Classes |
| Async Support | Limited / Hacky | Native Django 5.x Compatibility |
✨ Key Features
- Granular Triggers:
BEFORE_SAVE,AFTER_SAVE,BEFORE_CREATE,AFTER_CREATE,BEFORE_UPDATE,AFTER_UPDATE,BEFORE_DELETE,AFTER_DELETE. - Smart Conditions: Filter execution using
when,was,is_now, andhas_changed. - Transaction Safety: Native
on_commit=Truesupport ensures your side effects (emails, tasks) only fire if the database transaction persists. - Developer Experience: Auto-completion friendly and fully documented types.
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_lifecycle_hooks-1.0.0.tar.gz.
File metadata
- Download URL: django_lifecycle_hooks-1.0.0.tar.gz
- Upload date:
- Size: 7.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b07fd59543c1641c3f3cfb39567ab3a0166cebd1713b60713584f833374b125d
|
|
| MD5 |
a94debfcaa47bb9713182004f0b932d7
|
|
| BLAKE2b-256 |
484bf83077ad0cdb207d6c4116398590ed2c490f609d7fb0eab1c826a3bd231f
|
File details
Details for the file django_lifecycle_hooks-1.0.0-py3-none-any.whl.
File metadata
- Download URL: django_lifecycle_hooks-1.0.0-py3-none-any.whl
- Upload date:
- Size: 4.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
61bf64edff2b5a4de02272521816c6fdb3104d8c319e9c5003fbfe4e7c86fc2c
|
|
| MD5 |
d0aca5c07dfc8554ec553806225da6b7
|
|
| BLAKE2b-256 |
0bce06cf2513010837661b116f39f09fe62f09eacf56e4e1bb0402f1d497ad1b
|