Declarative model lifecycle hooks.
Project description
Django Lifecycle Hooks
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 unnesseary indirection and are at odds with Django's "fat models" approach.
In short, you can write model code like this:
from django_lifecycle import LifecycleModel, hook
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
0.6.0 (October 2019)
- Adds
when_anyhook paramter to watch multiple fields for state changes
0.5.0 (September 2019)
- Adds
was_notcondition - Allow watching changes to FK model field values, not just FK references
0.4.2 (July 2019)
- Fixes missing README.md issue that broke install.
0.4.1 (June 2019)
- Fixes urlman-compatability.
0.4.0 (May 2019)
- Fixes
initial_value(field_name)behavior - should return value even if no change. Thanks @adamJLev!
0.3.2 (February 2019)
- Fixes bug preventing hooks from firing for custom PKs. Thanks @atugushev!
0.3.1 (August 2018)
- Fixes m2m field bug, in which accessing auto-generated reverse field in
before_createcauses exception b/c PK does not exist yet. Thanks @garyd203!
0.3.0 (April 2018)
- Resets model's comparison state for hook conditions after
savecalled.
0.2.4 (April 2018)
- Fixed support for adding multiple
@hookdecorators to same method.
0.2.3 (April 2018)
- Removes residual mixin methods from earlier implementation.
0.2.2 (April 2018)
- Save method now accepts
skip_hooks, an optional boolean keyword argument that controls whether hooked methods are called.
0.2.1 (April 2018)
- Fixed bug in
_potentially_hooked_methodsthat caused unwanted side effects by accessing model instance methods decorated with@cache_propertyor@property.
0.2.0 (April 2018)
- Added Django 1.8 support. Thanks @jtiai!
- Tox testing added for Python 3.4, 3.5, 3.6 and Django 1.8, 1.11 and 2.0. Thanks @jtiai!
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
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
File details
Details for the file django-lifecycle-0.6.0.tar.gz.
File metadata
- Download URL: django-lifecycle-0.6.0.tar.gz
- Upload date:
- Size: 7.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/2.7.15+
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
de5000e2ccd1435ddd6872f12a9c237b0ea4077ac1f85dd7cb2bd2cf5f658b15
|
|
| MD5 |
c0618f24712f0ca3fd168c595ba3635b
|
|
| BLAKE2b-256 |
9bcb9c7ea329c8735a0acb8ab73186714efd542fc41e1eac6a03cdf289357088
|