Skip to main content

Declarative model lifecycle hooks.

Project description

Django Lifecycle Hooks

Package version Python versions Python versions PyPI - Django Version

This project provides a @hook decorator as well as a base model and mixin to add lifecycle hooks to your Django models. Django's built-in approach to offering lifecycle hooks is Signals. However, my team often finds that Signals introduce unnecessary indirection and are at odds with Django's "fat models" approach.

Django Lifecycle Hooks supports:

  • Python 3.7, 3.8, 3.9, 3.10, 3.11, and 3.12
  • Django 2.2, 3.2, 4.0, 4.1, 4.2, and 5.0

In short, you can write model code like this:

from django_lifecycle import LifecycleModel, hook, BEFORE_UPDATE, AFTER_UPDATE


class Article(LifecycleModel):
    contents = models.TextField()
    updated_at = models.DateTimeField(null=True)
    status = models.ChoiceField(choices=['draft', 'published'])
    editor = models.ForeignKey(AuthUser)

    @hook(BEFORE_UPDATE, WhenFieldHasChanged("contents", has_changed=True))
    def on_content_change(self):
        self.updated_at = timezone.now()

    @hook(
        AFTER_UPDATE, 
        condition=(
            WhenFieldValueWas("status", value="draft")
            & WhenFieldValueIs("status", value="published")
        )
    )
    def on_publish(self):
        send_email(self.editor.email, "An article has published!")

Instead of overriding save and __init__ in a clunky way that hurts readability:

    # same class and field declarations as above ...

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self._orig_contents = self.contents
        self._orig_status = self.status


    def save(self, *args, **kwargs):
        if self.pk is not None and self.contents != self._orig_contents:
            self.updated_at = timezone.now()

        super().save(*args, **kwargs)

        if self.status != self._orig_status:
            send_email(self.editor.email, "An article has published!")

Documentation: https://rsinger86.github.io/django-lifecycle

Source Code: https://github.com/rsinger86/django-lifecycle


Changelog

See Changelog

Testing

Tests are found in a simplified Django project in the /tests folder. Install the project requirements and do ./manage.py test to run them.

License

See License.

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-1.2.4.tar.gz (12.2 kB view details)

Uploaded Source

Built Distribution

django_lifecycle-1.2.4-py3-none-any.whl (15.0 kB view details)

Uploaded Python 3

File details

Details for the file django_lifecycle-1.2.4.tar.gz.

File metadata

  • Download URL: django_lifecycle-1.2.4.tar.gz
  • Upload date:
  • Size: 12.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.0 CPython/3.9.19

File hashes

Hashes for django_lifecycle-1.2.4.tar.gz
Algorithm Hash digest
SHA256 b37add8a95d0e85f9f97e652fac989cd5914cddb2380d933b6568f80238ab61e
MD5 3a4745c83f278ac8edb5421e80f8a674
BLAKE2b-256 9cac4e5ce8cb8b2eef726157bbfb6f227e79a4bf8e14d6346b21490b689464fb

See more details on using hashes here.

File details

Details for the file django_lifecycle-1.2.4-py3-none-any.whl.

File metadata

File hashes

Hashes for django_lifecycle-1.2.4-py3-none-any.whl
Algorithm Hash digest
SHA256 b54aea17b50de45adb5c90a06eea0171afa0d547682f51990dffb578b82fc658
MD5 e59bda2a6bc49bb2552ad4a6045c114d
BLAKE2b-256 ab3185dd0766c135e8688d88849fc3d245c783cab435cedfb6d9041a911ee22f

See more details on using hashes here.

Supported by

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