Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/rsinger86/django-lifecycle
Declarative model lifecycle hooks, an alternative to Signals.
https://github.com/rsinger86/django-lifecycle
django django-models lifecycle-hooks transition-conditions
Last synced: 4 days ago
JSON representation
Declarative model lifecycle hooks, an alternative to Signals.
- Host: GitHub
- URL: https://github.com/rsinger86/django-lifecycle
- Owner: rsinger86
- License: mit
- Created: 2018-03-18T20:53:33.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2024-11-27T08:23:26.000Z (24 days ago)
- Last Synced: 2024-12-10T04:44:25.014Z (11 days ago)
- Topics: django, django-models, lifecycle-hooks, transition-conditions
- Language: Python
- Homepage: https://rsinger86.github.io/django-lifecycle/
- Size: 1.51 MB
- Stars: 1,354
- Watchers: 18
- Forks: 90
- Open Issues: 22
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE.md
Awesome Lists containing this project
- best-of-web-python - GitHub - 26% open · ⏱️ 04.03.2024): (Django Utilities)
- -awesome-django - django-lifecycle - Declarative model lifecycle hooks, an alternative to Signals. (Third-Party Packages / Models)
- awesome-django - django-lifecycle - Declarative model lifecycle hooks, an alternative to Signals. (Third-Party Packages / Models)
- starred-awesome - django-lifecycle - Declarative model lifecycle hooks, inspired by Rails callbacks. (Python)
- stars - rsinger86/django-lifecycle - Declarative model lifecycle hooks, an alternative to Signals. (Python)
- stars - rsinger86/django-lifecycle - Declarative model lifecycle hooks, an alternative to Signals. (Python)
README
# Django Lifecycle Hooks
[![Package version](https://badge.fury.io/py/django-lifecycle.svg)](https://pypi.python.org/pypi/django-lifecycle)
[![Python versions](https://img.shields.io/pypi/status/django-lifecycle.svg)](https://img.shields.io/pypi/status/django-lifecycle.svg/)
[![Python versions](https://img.shields.io/pypi/pyversions/django-lifecycle.svg)](https://pypi.org/project/django-lifecycle/)
![PyPI - Django Version](https://img.shields.io/pypi/djversions/django-lifecycle)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](https://docs.djangoproject.com/en/dev/topics/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.0In short, you can write model code like this:
```python
from django_lifecycle import LifecycleModel, hook, BEFORE_UPDATE, AFTER_UPDATE
from django_lifecycle.conditions import WhenFieldValueIs, WhenFieldValueWas, WhenFieldHasChangedclass 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:
```python
# 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.statusdef 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](CHANGELOG.md)
# 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](LICENSE.md).