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, when='contents', has_changed=True)
    def on_content_change(self):
        self.updated_at = timezone.now()

    @hook(AFTER_UPDATE, when="status", was="draft", is_now="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.1.1.tar.gz (10.7 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-1.1.1-py3-none-any.whl (11.2 kB view details)

Uploaded Python 3

File details

Details for the file django-lifecycle-1.1.1.tar.gz.

File metadata

  • Download URL: django-lifecycle-1.1.1.tar.gz
  • Upload date:
  • Size: 10.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.18

File hashes

Hashes for django-lifecycle-1.1.1.tar.gz
Algorithm Hash digest
SHA256 904356d823b9636396ff96b338e65b6aeb2f9866940fa63d3c07f99b593544bd
MD5 b737948afbd8fb180486c4a4267095d4
BLAKE2b-256 f3bf877bfb051e24a57b56e27a981882a062024a59a719229098f757d9d5c6a3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for django_lifecycle-1.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 af69a9c2f6ab9f75ff41b6d56dd44ab11c0562fc1cdbc05a94a7514303374212
MD5 f10f66c22d6d7fdb5bc87f1f52c8e8cf
BLAKE2b-256 f1cb2b746b785039174f3b6a7b72d0210aae13ea5e772f10c901dcdc1f7d62f8

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