Skip to main content

A package for handling Django model signals.

This project has been archived.

The maintainers of this project have marked this project as archived. No new releases are expected.

Project description

Django Signal Handler Utility

This package provides a base class, SignalHandler, for simplifying the handling of Django model signals. It streamlines the process of responding to pre_save, post_save, pre_delete, and post_delete signals by categorizing them into more specific methods.

Installation

You can install this utility using pip:

pip install django-signal-handler

Usage

Create a subclass of SignalHandler:

import logging
from typing import Any

from django.db.models.signals import post_delete, post_save, pre_delete, pre_save

from signal_handler import SignalHandler
from your_app.models import MyModel

logger = logging.getLogger(__name__)

class MyModelSignalHandler(SignalHandler):
    def pre_save_new(self, instance: Any, **kwargs: Any) -> None:
        logger.info(f"Pre-save new: {instance}")
        # Add your logic here

    def post_save_new(self, instance: Any, **kwargs: Any) -> None:
        logger.info(f"Post-save new: {instance}")
        # Add your logic here

    def pre_save_update(self, instance: Any, **kwargs: Any) -> None:
        logger.info(f"Pre-save update: {instance}")
        # Add your logic here

    def post_save_update(self, instance: Any, **kwargs: Any) -> None:
        logger.info(f"Post-save update: {instance}")
        # Add your logic here

    def pre_delete(self, instance: Any, **kwargs: Any) -> None:
        logger.info(f"Pre-delete: {instance}")
        # Add your logic here

    def post_delete(self, instance: Any, **kwargs: Any) -> None:
        logger.info(f"Post-delete: {instance}")
        # Add your logic here

Connect the signal handlers to your model:

from django.dispatch import receiver
from your_app.models import MyModel

from .signal_handlers import MyModelSignalHandler

handler = MyModelSignalHandler()

@receiver(pre_save, sender=MyModel)
@receiver(post_save, sender=MyModel)
@receiver(pre_delete, sender=MyModel)
@receiver(post_delete, sender=MyModel)
def my_model_signals(sender, instance, **kwargs):
    handler.run(sender, instance, **kwargs)

Class Overview

SignalHandler

  • run(sender: type, instance: Any, **kwargs: Any) -> None:
    • Determines the signal type and dispatches to the appropriate handler method.
  • pre_save_new(instance: Any, **kwargs: Any) -> None:
    • Handler for pre_save signals when a new instance is being created.
  • post_save_new(instance: Any, **kwargs: Any) -> None:
    • Handler for post_save signals when a new instance is created.
  • pre_save_update(instance: Any, **kwargs: Any) -> None:
    • Handler for pre_save signals when an existing instance is being updated.
  • post_save_update(instance: Any, **kwargs: Any) -> None:
    • Handler for post_save signals when an existing instance is updated.
  • pre_delete(instance: Any, **kwargs: Any) -> None:
    • Handler for pre_delete signals.
  • post_delete(instance: Any, **kwargs: Any) -> None:
    • Handler for post_delete signals.

Benefits

  • Organization: Separates signal handling logic into distinct methods, improving code readability and maintainability.
  • Clarity: Makes it clear which signal is being handled (new vs. update for save signals).
  • Reusability: The base class can be extended for different models, reducing code duplication.
  • Logging: Includes basic logging for unknown or missing signals.

Contributing

Contributions are welcome! Please feel free to submit pull requests or open issues.

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-signal-handler-0.1.1.tar.gz (4.4 kB view details)

Uploaded Source

Built Distribution

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

django_signal_handler-0.1.1-py3-none-any.whl (4.7 kB view details)

Uploaded Python 3

File details

Details for the file django-signal-handler-0.1.1.tar.gz.

File metadata

  • Download URL: django-signal-handler-0.1.1.tar.gz
  • Upload date:
  • Size: 4.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.9

File hashes

Hashes for django-signal-handler-0.1.1.tar.gz
Algorithm Hash digest
SHA256 bfcab0f37cd559237774d2fe7ecb1c44cdf8c3e998c873a7a76bf4bb7cce58fb
MD5 6394825909f7403249fb72181ec7018e
BLAKE2b-256 70018169b23e2715b95eee9e82f2c9b1e4ec0db692f91925e4f2afa165412ec2

See more details on using hashes here.

File details

Details for the file django_signal_handler-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for django_signal_handler-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 c23208e3928224c5e969937374aa624742b9b7d4fd26769d1c8d2163e96b838f
MD5 fc746a777f9efcb8ba83643c78efec09
BLAKE2b-256 d703de2c201b3d6917ed2e98a17d4d807f1772ef8425cd5e954534daa48f9567

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