Skip to main content

Django signals that fire when specific model fields change

Project description

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).

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

django_fieldsignals-0.8.0.tar.gz (9.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_fieldsignals-0.8.0-py3-none-any.whl (9.8 kB view details)

Uploaded Python 3

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

Hashes for django_fieldsignals-0.8.0.tar.gz
Algorithm Hash digest
SHA256 c1850483383223bea1b6de48522352147c654af0b18ec75fbc6c3058f87d0ab5
MD5 96619b513dd28797eec43906ef8b70c0
BLAKE2b-256 39033c0abb4ed5d74ad85efc6814b938cf681d11b0e65c8aa69d9e8cf51a9b33

See more details on using hashes here.

File details

Details for the file django_fieldsignals-0.8.0-py3-none-any.whl.

File metadata

File hashes

Hashes for django_fieldsignals-0.8.0-py3-none-any.whl
Algorithm Hash digest
SHA256 89bc52e0967112bfaee7264561301810b3dfb59a3f8458468bbc3b899246520a
MD5 22eb1b1c232e86a8773ed49ddc38bc93
BLAKE2b-256 c6beefbd133fb6cd403b8e30197449d59c6c66fd98a02cdb0d762e5621cd492e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page