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).
Metadata
Release files for django-fieldsignals 0.8.0
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 | |
|---|---|---|---|---|
| django_fieldsignals-0.8.0.tar.gz | 9.0 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| django_fieldsignals-0.8.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 18.8 kB
Release files / django_fieldsignals-0.8.0.tar.gz
| Download URL | django_fieldsignals-0.8.0.tar.gz |
|---|---|
| Size | 9.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
c1850483383223bea1b6de48522352147c654af0b18ec75fbc6c3058f87d0ab5
|
|
BLAKE2b-256 checksum How to use checksums |
39033c0abb4ed5d74ad85efc6814b938cf681d11b0e65c8aa69d9e8cf51a9b33
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.12.3
|
Release files / django_fieldsignals-0.8.0-py3-none-any.whl
| Download URL | django_fieldsignals-0.8.0-py3-none-any.whl |
|---|---|
| Size | 9.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
89bc52e0967112bfaee7264561301810b3dfb59a3f8458468bbc3b899246520a
|
|
BLAKE2b-256 checksum How to use checksums |
c6beefbd133fb6cd403b8e30197449d59c6c66fd98a02cdb0d762e5621cd492e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.12.3
|
Security for django-fieldsignals 0.8.0
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.