Skip to main content

Build Status

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

  1. This library is on PyPI so you can install it with:
    pip install django-fieldsignals
  1. Add "fieldsignals" to your INSTALLED_APPS setting like this:
INSTALLED_APPS = (
    ...
    'fieldsignals',
)
  1. 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

  1. 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)
  1. Currently no support for ManyToManyField or reverse side of ForeignKey (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)

Source distribution for django-fieldsignals 0.8.0
File Size Uploaded
django_fieldsignals-0.8.0.tar.gz 9.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-fieldsignals 0.8.0
File Interpreter ABI Platform
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

Release history Release notifications | RSS feed

This release

0.8.0 This release

2 release files

0.7.0

2 release files

0.6.0

2 release files

0.5.0

2 release files

0.4.0

2 release files

0.3.4

2 release files

0.3.3

2 release files

0.3.2

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.3

2 release files

0.2.2

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page