docs
  • Overview
  • 🐍 PYTHON
    • Type Hints
    • PEP8 Style Guide for Python Code
    • 🏡Pipenv
    • Pathlib
  • 🕸Django
    • 🗄models
      • 🎯Best Practices
      • 🚦Django Signals
    • ⚙️ settings
    • DRF
      • Serializer
      • Authentication
      • Permissions
      • Viewsets
    • Testing
      • Faker and Factory Boy
    • 🧪Test Coverage
    • 💦Python-Decouple
    • Django Tips:
    • 💾Django ORM Queryset
    • Custom Exceptions
    • Celery
    • Resources
  • Deploy
    • 🚀Django Deployment
    • 🔒Setup SSL Certificate
  • 💾Database
    • MongoDB
  • 🛠️DevOps
    • 🖥Scripting
      • A First Script
      • Loops
      • Test
      • Variables
      • External programs
      • Functions
    • Command Line Shortcuts
    • Basic Linux Commands
    • 🎛Microservices
    • 🐳Docker
      • Docker Commands
      • Docker Compose
      • Django project
    • Kubernates
  • 📝Software IDE
    • EditorConfig
    • Linters
    • VsCode
Powered by GitBook
On this page

Was this helpful?

  1. 🕸Django
  2. 🗄models

Django Signals

Django Signals like pro

Django Signals

Django signals are extremely useful for decoupling modules. The allow a low-level Django app to send events for the other to handle without creating a direct dependency.

# signals.py

from django.dispatch import Signal

charge_completed = Signal(providing_args=['total'])


# payment.py

from .signals import charge_completed

@classmethod
def processc_charge(cls, total):

    # Process charge ...
    
    if success:
        charge_completed.send_robust(sender=cls, total=total)

# A different app, such as a summary app, can connect a handler that
# increments a total charges counter:

# summary.py

from django.dispatch import reciver

from .signals import charge_completed

@reciver(charge_completed)
def increment_total_charges(sender, total, **kwargs):
    total_charges += total

For example, the following are good candidates for receivers:

  • Update the transaction status.

  • Send an email notification to the user.

  • Update the last used date of the credit card.

Previous🎯Best PracticesNext⚙️ settings

Last updated 3 years ago

Was this helpful?

🚦