Skip to main content

django-db-signals

This app adds a set of signals to some of Django’s database operations:

  • django.db.signals.pre_commit

  • django.db.signals.post_commit

  • django.db.signals.pre_rollback

  • django.db.signals.post_rollback

  • django.db.signals.pre_transaction_management

  • django.db.signals.post_transaction_management

Requirements

  • Python 2.6/2.7/3.2/3.3 (3.x requires Django >=1.5)

  • Django 1.2/1.3/1.4/1.5

Installation

  1. Install from PyPI: pip install django-db-signals.

  2. Add 'django_db_signals' to INSTALLED_APPS.

  3. Enable the signals by adding to your models.py:

    import django_db_signals
    django_db_signals.enable()

Example

Let’s assume you’ve installed django-db-signals, and now you’d like to log a message each time the database is rolled back:

from django.db import signals
from django.dispatch import receiver
import logging

logger = logging.getLogger(__name__)

@reciever(signals.post_rollback)
def log_rollbacks(sender, **kwargs):
    # sender is a DatabaseWrapper object
    logger.info("A rollback occurred on database %s" %
                sender.alias)

Design

In the same way that Django settings are available via django.conf.settings attributes, signals are available via django.db.signals attributes. As such, attempting to import individual signals will fail:

>>> import django_db_signals
>>> django_db_signals.enable()

>>> from django.db import signals  # GOOD
>>> signals.pre_commit
<django.dispatch.dispatcher.Signal object at 0x1089c8b90>

>>> from django.db.signals import pre_commit  # BAD
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named signals

pre_… vs post_… signals

pre_… signals are sent before an operation occurs. The signals are sent via .send(…). Exceptions raised in receivers are propagated to the application. This can be exploited to cancel the operation (e.g. to block a commit).

post_… signals are sent after an operation, and as such can’t offer the same cancel the pending operation behaviour. The signal is sent via .send_robust(…) to ensure all receivers are called. Any exceptions raised are logged, but are not propagated to the application.

Logging

A logger named django.db.signals is used to log all exceptions raised in post_… receivers.

Signal senders

For all database signals, the sender of the signal is Django’s database connection wrapper.

Troubleshooting

“I can import django.db.signals, but when I try to access a signal I get an AttributeError exception.”

You need to enable the signals via django_db_signals.enable()

Metadata

Release files for django-db-signals 0.1.1

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-db-signals 0.1.1
File Size Uploaded Provenance Actions
django-db-signals-0.1.1.tar.gz 3.8 kB Without provenance Details for django-db-signals-0.1.1.tar.gz

Release files / django-db-signals-0.1.1.tar.gz

Download URL django-db-signals-0.1.1.tar.gz
Size 3.8 kB
Tags Source
SHA-256 checksum
How to use checksums
b2e4b0b6a471ed6cfb3f78650b518af92b2e62ba0c4714c7062debd1aa40a333
BLAKE2b-256 checksum
How to use checksums
ba72708ed89361d08679dd04d5783af51ee1e196e4b026493f70c59e4f77b963
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Security for django-db-signals 0.1.1

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.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.1 This release

1 release file No provenance

0.1.0

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