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

Last updated

Was this helpful?