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 a
ModelSignalsMetainner class to your Django model and specify which signals you want to connect. - To enable the
pre_bulk_saveandpost_bulk_savesignals, add theModelSignalsManagerto your Django model'sobjectsproperty. - To enable the
pre_full_clean,post_full_cleanandpost_full_clean_errorsignals, inherit from theFullCleanSignalsMixinin your Django model. - To enable the
post_save_errorsignal, inherit from thePostSaveErrorSignalMixinin your Django model. - Implement the receiver methods for the connected signals in your Django model.
Example
from django.db.models import Model
from django_model_signals.manager import ModelSignalsManager
from django_model_signals.models import (
FullCleanSignalsMixin,
PostSaveErrorSignalMixin
)
class MyModel(
FullCleanSignalsMixin,
PostSaveErrorSignalMixin,
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 post_full_clean_error(self, **kwargs):
raise kwargs['error']
def pre_save(self, **kwargs):
pass
def post_save(self, **kwargs):
pass
def post_save_error(self, **kwargs):
raise kwargs['error']
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',
'post_full_clean_error',
'pre_save',
'post_save',
'post_save_error',
'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_initandpost_initsignals. - Calling
Model.full_cleanwill trigger thepre_full_cleanandpost_full_cleansignals. - An error during
Model.full_cleanwill trigger thepost_full_clean_errorsignal. - Calling
Model.savewill trigger thepre_saveandpost_savesignals. - An error during
Model.savewill trigger thepost_save_errorsignal. - Calling
Model.deletewill trigger thepre_deleteandpost_deletesignals. - Calling
Model.objects.createwill trigger thepre_saveandpost_savesignals. - Calling
Model.objects.get_or_createwill trigger thepre_saveandpost_savesignals. - Calling
Model.objects.update_or_createwill trigger thepre_saveandpost_savesignals. - Calling
Model.objects.bulk_createwill trigger thepre_bulk_saveandpost_bulk_savesignals. - Calling
Model.objects.bulk_updatewill trigger thepre_bulk_saveandpost_bulk_savesignals. - Calling
QuerySet.deletewill trigger thepre_deleteandpost_deletesignals.
- Creating or loading an model instance from the database will trigger the
- To implement the
pre_full_cleanandpost_full_clean,post_full_clean_errorandpost_save_errorssignals, this library overrides thefull_cleanandsavemethods 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. - The
post_full_clean_errorandpost_save_errorsignals are not actually called as signals, but the receiver methods are called directly. This allows you to suppress, change or re-raise the error.
Resources
- Django: https://www.djangoproject.com/
Metadata
Release files for django-model-signals 0.4.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| django_model_signals-0.4.3.tar.gz | 4.3 kB | Without provenance | Details for django_model_signals-0.4.3.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| django_model_signals-0.4.3-py3-none-any.whl | Python 3 | none | any | Details for django_model_signals-0.4.3-py3-none-any.whl |
Total release size: 10.2 kB
Release files / django_model_signals-0.4.3.tar.gz
| Download URL | django_model_signals-0.4.3.tar.gz |
|---|---|
| Size | 4.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
f87cf6ea6e7381c7d028e6f95c885b340a90bb6fa84cd41f5152476924ac40fe
|
|
BLAKE2b-256 checksum How to use checksums |
bcebab58640a4ad615a40bcb83e61d7e9c83dcab2f9d06da05a0afe9497ae1b7
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.9.18
|
Release files / django_model_signals-0.4.3-py3-none-any.whl
| Download URL | django_model_signals-0.4.3-py3-none-any.whl |
|---|---|
| Size | 5.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
0f4188c9736ccaa9a5c118f3496c33e8eb205919122fd5450a0b237b3cbc2177
|
|
BLAKE2b-256 checksum How to use checksums |
99a4b88b5094c5711e84919ae073f2092edd6d55cf6109011373bf16e844d910
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.9.18
|
Security for django-model-signals 0.4.3
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.