A next-generation, high-performance declarative lifecycle hooks library for Django 5.x+
Project description
Django Lifecycle Hooks
The high-performance, zero-overhead alternative to Django Signals.
⚡ Why this library?
Stop slowing down your Django app with inefficient signals.
Most lifecycle libraries (and Django's own signals) rely on heavy runtime introspection, full object copying, and loose coupling that makes debugging a nightmare.
Django Lifecycle Hooks is engineered for performance-critical applications:
- 🚀 Zero Runtime Overhead: Hook resolution happens once at import time. Executing hooks is an O(1) operation.
- ⚡ First-Class Async & ASGI Support: The only lifecycle library with native
asaveandacreatesupport. We don't just wrap sync code; we await your async hooks properly. - 💾 Sparse Snapshotting: We don't copy your entire model. If you only watch
status, we only cachestatus. Your memory usage stays flat. - 🛡️ Type-Safe & Modern: Built for Python 3.14+ and Django 5.2+. Fully typed,
__slots__optimized, and ready for strict MyPy validation. - ✨ Developer Joy: No more hunting for
@receiverin random files. Logic lives where it belongs: inside your model or specialized hooks.
🏎️ Performance Comparison
| Feature | Standard Signals / Legacy Libs | Django Lifecycle Hooks |
|---|---|---|
| Hook Resolution | Runtime Introspection (Slow) | Import-time Registry (Instant) |
| Change Detection | Full __dict__ copy (High RAM) |
Sparse Field Copy (Low RAM) |
| Lookup Speed | O(n) Listener Loop | O(1) Direct Dispatch |
| Async / ASGI | ❌ None or Hacky Wrappers | ✅ Native asave & acreate Support |
🚀 Quick Start
1. Install
pip install django-lifecycle-hooks
2. Write Elegant Code
Inherit from LifecycleModelMixin and declare your logic.
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")
# ✅ 1. Simple & Fast
@hook(HookType.BEFORE_SAVE)
def clean_username(self):
self.username = self.username.lower()
# ✅ 2. Conditional & Declarative
# Only runs when status changes to 'banned'.
# No "if" checks needed inside your method.
@hook(HookType.AFTER_UPDATE, when="status", was="active", is_now="banned")
def on_ban(self):
self.ban_related_assets()
# ✅ 3. Transaction Safe
# Only fires after the DB transaction commits successfully.
@hook(HookType.AFTER_SAVE, on_commit=True)
def send_welcome_email(self):
send_email_task.delay(self.id)
📚 Documentation
- Documentation: Master advanced patterns (Async, Stacked Hooks, Conditions).
Built for engineers who care about speed, clean code, and type safety ♥️
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.2.tar.gz.
File metadata
- Download URL: django_lifecycle_hooks-1.0.2.tar.gz
- Upload date:
- Size: 20.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b4125d0f2ac9b8a148234881b18251977cae5e2b7f48b34f31816856d1a13809
|
|
| MD5 |
97c48c6e33983da625b68a63a973ec4d
|
|
| BLAKE2b-256 |
b804214b9c6b0a4aa6efc8377f7aa06fd49e79bc8776f7b36ec357ca3f53bc1f
|
File details
Details for the file django_lifecycle_hooks-1.0.2-py3-none-any.whl.
File metadata
- Download URL: django_lifecycle_hooks-1.0.2-py3-none-any.whl
- Upload date:
- Size: 13.7 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 |
4b06c858cb780c669802127a4189b1d2aff1a83b308275757f7168908dd72177
|
|
| MD5 |
463c724badb14db40d112ac6efe14878
|
|
| BLAKE2b-256 |
f862ef200f1db60df64b823c11ac2e29a4ebbfacedad09af05daf033d2d75f64
|