All Projects → rsinger86 → Django Lifecycle

rsinger86 / Django Lifecycle

Licence: mit
Declarative model lifecycle hooks, an alternative to Signals.

Programming Languages

python
139335 projects - #7 most used programming language

Labels

Projects that are alternatives of or similar to Django Lifecycle

Graphite Web
A highly scalable real-time graphing system
Stars: ✭ 5,384 (+701.19%)
Mutual labels:  django
Python Spider
豆瓣电影top250、斗鱼爬取json数据以及爬取美女图片、淘宝、有缘、CrawlSpider爬取红娘网相亲人的部分基本信息以及红娘网分布式爬取和存储redis、爬虫小demo、Selenium、爬取多点、django开发接口、爬取有缘网信息、模拟知乎登录、模拟github登录、模拟图虫网登录、爬取多点商城整站数据、爬取微信公众号历史文章、爬取微信群或者微信好友分享的文章、itchat监听指定微信公众号分享的文章
Stars: ✭ 615 (-8.48%)
Mutual labels:  django
Django Markdownx
Comprehensive Markdown plugin built for Django
Stars: ✭ 657 (-2.23%)
Mutual labels:  django
Django blog tutorial
Django搭建博客教程
Stars: ✭ 599 (-10.86%)
Mutual labels:  django
Django River
Django workflow library that supports on the fly changes ⛵
Stars: ✭ 609 (-9.37%)
Mutual labels:  django
Django Graphql Jwt
JSON Web Token (JWT) authentication for Graphene Django
Stars: ✭ 649 (-3.42%)
Mutual labels:  django
Awesome Django
The Best Django Resource, Awesome Django for mature packages.
Stars: ✭ 591 (-12.05%)
Mutual labels:  django
old vespene
DISCONTINUED: a frozen fork will exist forever at mpdehaan/vespene
Stars: ✭ 672 (+0%)
Mutual labels:  django
Django Stubs
PEP-484 stubs for Django
Stars: ✭ 611 (-9.08%)
Mutual labels:  django
Python24
网上搜集的自学python语言的资料集合,包括整套代码和讲义集合,这是至今为止所开放网上能够查找到的最新视频教程,网上找不到其他最新的python整套视频了,. 具体的无加密的mp4视频教程和讲义集合可以在更新的Readme文件中找到,下载直接打开就能播放,项目从零基础的Python教程到深度学习,总共30章节,其中包含Python基础中的飞机大战项目,WSGI项目,Flask新经资讯项目, Django的电商项目(本应该的美多商城项目因为使用的是Vue技术,所以替换为Django天天生鲜项目)等等,希望能够帮助大家。资源搜集劳神费力,能帮到你的话是我的福分,望大家多多支持,喜欢本仓库的话,记得Star哦。
Stars: ✭ 650 (-3.27%)
Mutual labels:  django
Django Newsletter
An email newsletter application for the Django web application framework, including an extended admin interface, web (un)subscription, dynamic e-mail templates, an archive and HTML email support.
Stars: ✭ 605 (-9.97%)
Mutual labels:  django
Moviegeek
A django website used in the book Practical Recommender Systems to illustrate how recommender algorithms can be implemented.
Stars: ✭ 608 (-9.52%)
Mutual labels:  django
Pinax Stripe
a payments Django app for Stripe
Stars: ✭ 650 (-3.27%)
Mutual labels:  django
Django Paypal
A pluggable Django application for integrating PayPal Payments Standard or Payments Pro
Stars: ✭ 602 (-10.42%)
Mutual labels:  django
Django Dashing
django-dashing is a customisable, modular dashboard application framework for Django to visualize interesting data about your project. Inspired in the dashboard framework Dashing
Stars: ✭ 660 (-1.79%)
Mutual labels:  django
Photonix
This is a new web-based photo management application. Run it on your home server and it will let you find the right photo from your collection on any device. Smart filtering is made possible by object recognition, location awareness, color analysis and other ML algorithms.
Stars: ✭ 592 (-11.9%)
Mutual labels:  django
Dwitter
Social network for short js demos
Stars: ✭ 618 (-8.04%)
Mutual labels:  django
Aws Boilerplate
Opinionated full stack web app's boilerplate, ready to be deployed to AWS platform.
Stars: ✭ 682 (+1.49%)
Mutual labels:  django
Modern Django
Modern Django: A Guide on How to Deploy Django-based Web Applications in 2017
Stars: ✭ 662 (-1.49%)
Mutual labels:  django
Django Fiber
Django Fiber - a simple, user-friendly CMS for all your Django projects
Stars: ✭ 653 (-2.83%)
Mutual labels:  django

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.5, 3.6, 3.7, 3.8 and 3.9, Django 2.0.x, 2.1.x, 2.2.x, 3.0.x and 3.1.x.

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

0.9.1 (March 2021)

  • Makes hooks work with OneToOneFields. Thanks @bahmdev!

0.9.0 (February 2021)

  • Prevents calling a hooked method twice with the same state. Thanks @garyd203!

0.8.1 (January 2021)

  • Added missing return to delete() method override. Thanks @oaosman84!

0.8.0 (October 2020)

  • Significant performance improvements. Thanks @dralley!

0.7.7 (August 2020)

  • Fixes issue with GenericForeignKey. Thanks @bmbouter!

0.7.6 (May 2020)

  • Updates to use constants for hook names; updates docs to indicate Python 3.8/Django 3.x support. Thanks @thejoeejoee!

0.7.5 (April 2020)

  • Adds static typed variables for hook names; thanks @Faisal-Manzer!
  • Fixes some typos in docs; thanks @tomdyson and @bmispelon!

0.7.1 (January 2020)

  • Fixes bug in utils._get_field_names that could cause recursion bug in some cases.

0.7.0 (December 2019)

  • Adds changes_to condition - thanks @samitnuk! Also some typo fixes in docs.

0.6.1 (November 2019)

  • Remove variable type annotation for Python 3.5 compatability.

0.6.0 (October 2019)

  • Adds when_any hook parameter to watch multiple fields for state changes

0.5.0 (September 2019)

  • Adds was_not condition
  • 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)

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_create causes exception b/c PK does not exist yet. Thanks @garyd203!

0.3.0 (April 2018)

  • Resets model's comparison state for hook conditions after save called.

0.2.4 (April 2018)

  • Fixed support for adding multiple @hook decorators 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_methods that caused unwanted side effects by accessing model instance methods decorated with @cache_property or @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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].