Django signals that fire when specific model fields change
Project description
Introduction
django-fieldsignals simply makes it easy to tell when the fields on your model have changed.
Often model updates are quite expensive. Sometimes the expensive operations are very rare. This makes it tempting to put the update logic in a view, rather than in a save() method or in a signal receiver:
# A bad example. Don't do this!
def edit_poll(request, poll_id):
# ...
if form.cleaned_data['poll_name'] != poll.name:
poll.update_slug(form.cleaned_data['poll_name'])
poll.save()
That's a bad idea, because your model consistency is now dependent on your view.
Instead, use django-fieldsignals:
from django.dispatch import receiver
from fieldsignals import pre_save_changed
@receiver(pre_save_changed, sender=Poll, fields=['name'])
def update_poll_slug(sender, instance, **kwargs):
instance.slug = slugify(instance.name)
In case you want to know what changed, django-fieldsignals even tells you the old and new values of your fields:
@receiver(pre_save_changed, sender=Poll)
def print_all_field_changes(sender, instance, changed_fields, **kwargs):
for field_name, (old, new) in changed_fields.items():
print(f'{field_name} changed from {old} to {new}')
Installation
- This library is on PyPI so you can install it with:
pip install django-fieldsignals
- Add
"fieldsignals"to yourINSTALLED_APPSsetting like this:
INSTALLED_APPS = (
...
'fieldsignals',
)
- Add some signals!
Where should my signals code live?
Field signals must be connected after the django apps are ready. So putting signal connectors at the bottom of your models file, or other random places won't work.
The best place to connect fieldsignals is an AppConfig.ready() handler.
Notes
- fieldsignals signals do not trigger if your fields don't change between instantiation and
save(). That is:
# This will NOT trigger post_save_changed
instance = MyModel.objects.create(field1='x')
# This will also NOT trigger post_save_changed
instance = MyModel(field1='x')
instance.save()
# But this WILL trigger post_save_changed
instance = MyModel()
instance.field1 = 'x'
instance.save()
If you want to also trigger your signals during creation, register your handler as a regular signal also, and check the created kwarg:
@receiver(pre_save, sender=Poll)
@receiver(pre_save_changed, sender=Poll, fields=['name'])
def update_poll_slug(sender, instance, *, created, changed_fields=None, **kwargs):
if created or changed_fields:
instance.slug = slugify(instance.name)
- Currently no support for
ManyToManyFieldor reverse side ofForeignKey(one to many).
Project details
Release history Release notifications | RSS feed
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file django_fieldsignals-0.8.0.tar.gz.
File metadata
- Download URL: django_fieldsignals-0.8.0.tar.gz
- Upload date:
- Size: 9.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c1850483383223bea1b6de48522352147c654af0b18ec75fbc6c3058f87d0ab5
|
|
| MD5 |
96619b513dd28797eec43906ef8b70c0
|
|
| BLAKE2b-256 |
39033c0abb4ed5d74ad85efc6814b938cf681d11b0e65c8aa69d9e8cf51a9b33
|
File details
Details for the file django_fieldsignals-0.8.0-py3-none-any.whl.
File metadata
- Download URL: django_fieldsignals-0.8.0-py3-none-any.whl
- Upload date:
- Size: 9.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
89bc52e0967112bfaee7264561301810b3dfb59a3f8458468bbc3b899246520a
|
|
| MD5 |
22eb1b1c232e86a8773ed49ddc38bc93
|
|
| BLAKE2b-256 |
c6beefbd133fb6cd403b8e30197449d59c6c66fd98a02cdb0d762e5621cd492e
|