Skip to main content

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.

PyPI version Python Versions Django Versions License: MIT


⚡ 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 asave and acreate support. 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 cache status. 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 @receiver in 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_lifecycle_hooks-1.0.2.tar.gz (20.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_lifecycle_hooks-1.0.2-py3-none-any.whl (13.7 kB view details)

Uploaded Python 3

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

Hashes for django_lifecycle_hooks-1.0.2.tar.gz
Algorithm Hash digest
SHA256 b4125d0f2ac9b8a148234881b18251977cae5e2b7f48b34f31816856d1a13809
MD5 97c48c6e33983da625b68a63a973ec4d
BLAKE2b-256 b804214b9c6b0a4aa6efc8377f7aa06fd49e79bc8776f7b36ec357ca3f53bc1f

See more details on using hashes here.

File details

Details for the file django_lifecycle_hooks-1.0.2-py3-none-any.whl.

File metadata

File hashes

Hashes for django_lifecycle_hooks-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 4b06c858cb780c669802127a4189b1d2aff1a83b308275757f7168908dd72177
MD5 463c724badb14db40d112ac6efe14878
BLAKE2b-256 f862ef200f1db60df64b823c11ac2e29a4ebbfacedad09af05daf033d2d75f64

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page