Django Model Signals makes it easier to keep model related business logic in your Django models by allowing them to become transceivers of their own signals, including bulk signals.
Project description
Django Model Signals
Django Model Signals makes it easier to keep business logic in your Django models by allowing them to become transceivers of their own signals, including bulk signals.
Installation
pip install django-model-signals
Configuration
Add the django_model_signals
app to your INSTALLED_APPS
:
INSTALLED_APPS = [
# ...
'django_model_signals',
]
Usage
- Add the
ModelSignalsTransceiver
class to your Django model. - Add a
ModelSignalsMeta
inner class to your Django model and specify which signals you're interested in. - Add the
ModelSignalsManager
to your Django model'sobjects
property to enable bulk signals. - Implement the signal receiver methods in your Django model.
Example
from django.db.models import Model
from django_model_signals.manager import ModelSignalsManager
from django_model_signals.transceiver import ModelSignalsTransceiver
class MyModel(
ModelSignalsTransceiver,
Model
):
@classmethod
def pre_init(cls, **kwargs):
pass
def post_init(self, **kwargs):
pass
def pre_full_clean(self, **kwargs):
pass
def post_full_clean(self, **kwargs):
pass
def pre_save(self, **kwargs):
pass
def post_save(self, **kwargs):
pass
def pre_delete(self, **kwargs):
pass
def post_delete(self, **kwargs):
pass
def m2m_changed(self, **kwargs):
pass
@classmethod
def pre_bulk_save(cls, **kwargs):
pass
@classmethod
def post_bulk_save(cls, **kwargs):
pass
objects = ModelSignalsManager()
class ModelSignalsMeta:
signals = [
'pre_init',
'post_init',
'pre_full_clean',
'post_full_clean',
'pre_save',
'post_save',
'pre_delete',
'post_delete',
'm2m_changed',
'pre_bulk_save',
'post_bulk_save'
]
Notes
-
The following actions are supported for triggering the implemented signals:
- Creating or loading an model instance from the database will trigger the
pre_init
andpost_init
signals. - Calling
Model.full_clean
will trigger thepre_full_clean
andpost_full_clean
signals. - Calling
Model.save
will trigger thepre_save
andpost_save
signals. - Calling
Model.delete
will trigger thepre_delete
andpost_delete
signals. - Calling
Model.objects.create
will trigger thepre_save
andpost_save
signals. - Calling
Model.objects.get_or_create
will trigger thepre_save
andpost_save
signals. - Calling
Model.objects.update_or_create
will trigger thepre_save
andpost_save
signals. - Calling
Model.objects.bulk_create
will trigger thepre_bulk_save
andpost_bulk_save
signals. - Calling
Model.objects.bulk_update
will trigger thepre_bulk_save
andpost_bulk_save
signals. - Calling
QuerySet.delete
will trigger thepre_delete
andpost_delete
signals.
- Creating or loading an model instance from the database will trigger the
-
To implement the
pre_full_clean
andpost_full_clean
signals, this library overrides thefull_clean
method of Django models and calls the original method in a backwards compatible way. However, make sure the order of the classes inherited from is the same as the above example to ensure the proper method resolution order.
Resources
- Django: https://www.djangoproject.com/
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
Built Distribution
Hashes for django_model_signals-0.3.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3d5a8fd1a3710a79aa8ccf55523190fc50288573e1cc0f27473ab85af75e2d1d |
|
MD5 | 787bf3f96e83535c0d2447ac5e737748 |
|
BLAKE2b-256 | 0ab7d36089bb419f01286759810fd92db820158c9094a3098a22eb03567d2596 |
Hashes for django_model_signals-0.3.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 555843e3f561b4dea387c18a9713f65f7d71ff1225251bcf2a9e741c682dc07e |
|
MD5 | bae739a7c1856b474b29a1d34e6fcd67 |
|
BLAKE2b-256 | 94a968ebc0177e02c5cbac5d424e30bda2a63dfb544380e444dc0bedff2a7010 |