Skip to main content

Django fieldsignals simply makes it easy to tell when the fields on your model have changed.

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 fieldsignals import pre_save_changed

    def update_poll_slug(sender, instance, **kwargs):
        instance.slug = slugify(instance.name)

    pre_save_changed.connect(update_poll_slug, sender=Poll, fields=['name'])

In case you want to know what changed, django-fieldsignals even tells you the old and new values of your fields:

    from fieldsignals import pre_save_changed

    def print_all_field_changes(sender, instance, changed_fields, **kwargs):
        for field, (old, new) in changed_fields.items():
            print(f'{field.name} changed from {old} to {new}')

    pre_save_changed.connect(print_all_field_changes, sender=Poll)

Installation

  1. This library is on PyPI so you can install it with:
    pip install django-fieldsignals

or from github:

    pip install 'git+https://github.com/craigds/django-fieldsignals.git#egg=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

  • 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.7.0.tar.gz (7.7 kB view details)

Uploaded Source

Built Distribution

django_fieldsignals-0.7.0-py2.py3-none-any.whl (8.0 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file django-fieldsignals-0.7.0.tar.gz.

File metadata

  • Download URL: django-fieldsignals-0.7.0.tar.gz
  • Upload date:
  • Size: 7.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.25.1 setuptools/51.1.1 requests-toolbelt/0.9.1 tqdm/4.48.0 CPython/3.8.7

File hashes

Hashes for django-fieldsignals-0.7.0.tar.gz
Algorithm Hash digest
SHA256 2a023b03530ec26f606339addce687ec67294cff10ffaf9f493927de80e25040
MD5 e1d955db92757c77a11b21cd35788937
BLAKE2b-256 0644e14de035e85209671383dcf157b8bededd289e7ee4f581f1ac4282ea5dc4

See more details on using hashes here.

File details

Details for the file django_fieldsignals-0.7.0-py2.py3-none-any.whl.

File metadata

  • Download URL: django_fieldsignals-0.7.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 8.0 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.25.1 setuptools/51.1.1 requests-toolbelt/0.9.1 tqdm/4.48.0 CPython/3.8.7

File hashes

Hashes for django_fieldsignals-0.7.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 a692f94436b416e604104ec8de1ec979d28efe30647f964c15a661dcf1ca3198
MD5 ebad35d689775d62a4ceb15d5106240e
BLAKE2b-256 cfdaf9bee4e55b6cbf3a9c4bb093303575e9a3f18a23ff31c9f2007a263a1c72

See more details on using hashes here.

Supported by

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