tj-django / django-model-subscription

Subscribe to django model changes. Using thread safe subscribers.
https://django-model-subscription.readthedocs.io/en/latest/installation.html
MIT License
29 stars 6 forks source link
auto-discovery bulk django django-model django-model-subscribers django-signals observer-pattern

PyPI Actions Status Documentation Status

Codacy Badge Codacy Badge codecov PyPI - License pre-commit.ci status

PyPI - Python Version PyPI - Django Version Downloads

django-model-subscription

Subscribe to django model changes include bulk create/update/delete.

Table of contents

Features

Subscriber

Installation

$ pip install django-model-subscription

Add model_subscription to your INSTALLED_APPS

INSTALLED_APPS = [
    ...,
    'model_subscription',
    ...
]

Usage

Using the SubscriptionModelMixin and SubscriptionQuerySet
from model_subscription.mixin import SubscriptionModelMixin
from model_subscription.model import SubscriptionQuerySet

class TestModel(SubscriptionModelMixin, models.Model):
    name = models.CharField(max_length=255)

    objects = SubscriptionQuerySet.as_manager()
Subclassing the SubscriptionModel base class.
from model_subscription.model import SubscriptionModel

class TestModel(SubscriptionModel):
    name = models.CharField(max_length=255)

Creating subscribers.

import logging
from model_subscription.decorators import subscribe
from model_subscription.constants import OperationType

log = logging.getLogger(__name__)

@subscribe(OperationType.CREATE, TestModel)
def handle_create(instance):
    log.debug('Created {}'.format(instance.name))

import logging
from model_subscription.decorators import create_subscription

log = logging.getLogger(__name__)

@create_subscription(TestModel)
def handle_create(instance):
    log.debug('Created {}'.format(instance.name))

Decorators

(Create, Update, Delete) operations.

@create_subscription(TestModel)
def handle_create(instance):
    log.debug('1. Created {}'.format(instance.name))
@update_subscription(TestModel)
def handle_update(instance, changed_data):
    log.debug('Updated {} {}'.format(instance.name, changed_data))

NOTE: The instance.pk is already set to None.

@delete_subscription(TestModel)
def handle_delete(instance):
    log.debug('Deleted {}'.format(instance.name))

(Bulk Create, Bulk Update, Bulk Delete) operations.


@bulk_create_subscription(TestModel)
def handle_bulk_create(instances):
    for instance in instances:
        log.debug('Bulk Created {}'.format(instance.name))
@bulk_update_subscription(TestModel)
def handle_bulk_update(instances):
    for instance in instances:
        log.debug('Updated {}'.format(instance.name))

@bulk_delete_subscription(TestModel)
def handle_bulk_delete(instances):
    for instance in instances:
        log.debug('Deleted {}'.format(instance.name))

Setup Subscribers using AppConfig.ready (Recomended).

Update you apps.py


from django.apps import AppConfig

class MyAppConfig(AppConfig):
    name = 'myapp'

    def ready(self):
        from myapp import subscriptions

Setup Subscribers using auto discovery.

By default the settings.SUBSCRIPTION_AUTO_DISCOVER is set to False.

To use auto discovery this is not recommended as it would notify the subscribers wherever the model is used i.e IPython notebook, external scripts.

In your settings.py add


SUBSCRIPTION_AUTO_DISCOVER = True

Setting up the SUBSCRIPTION_MODULE

NOTE: This is only required when SUBSCRIPTION_AUTO_DISCOVER = True


SUBSCRIPTION_MODULE  = 'subscription'

Credits

If you feel generous and want to show some extra appreciation:

Buy me a coffee

Resources

TODO's