0
0
Djangoframework~10 mins

When signals are appropriate vs not in Django - Visual Side-by-Side Comparison

Choose your learning style9 modes available
Concept Flow - When signals are appropriate vs not
Event occurs in app
Signal sent
Signal receivers listen
Receivers execute code
Side effects happen
App continues normal flow
Signals let parts of a Django app react to events without tight connections, but they should be used only for decoupled side effects.
Execution Sample
Django
from django.db.models.signals import post_save
from django.dispatch import receiver

@receiver(post_save, sender=MyModel)
def my_handler(sender, instance, created, **kwargs):
    if created:
        print('New instance created')
This code listens for when a new MyModel instance is saved and prints a message only if it is newly created.
Execution Table
StepEventSignal Sent?Receiver Called?Receiver ActionOutput
1MyModel instance saved (new)YesYesCheck if created=TruePrint 'New instance created'
2MyModel instance saved (update)YesYesCheck if created=FalseNo output
3Unrelated eventNoNoNo actionNo output
4Signal sent but no receiver connectedYesNoNo actionNo output
💡 Execution stops after signal receivers finish or if no receivers are connected.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4
instance.createdN/ATrueFalseN/AN/A
signal_sentFalseTrueTrueFalseTrue
receiver_calledFalseTrueTrueFalseFalse
Key Moments - 3 Insights
Why doesn't the receiver run if the signal is sent but no receiver is connected?
Because signals only trigger code in connected receivers. If no receiver listens, no code runs, as shown in step 4 of the execution table.
Why check if 'created' is True inside the receiver?
Because post_save runs on both create and update. Checking 'created' ensures the receiver acts only on new instances, as in steps 1 and 2.
When should you avoid using signals?
Avoid signals for logic that should be explicit or tightly coupled, since signals can make code harder to follow and debug.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, at which step does the receiver print 'New instance created'?
AStep 2
BStep 1
CStep 3
DStep 4
💡 Hint
Check the 'Output' column for the print statement in the execution table.
According to the variable tracker, what is the value of 'receiver_called' after step 4?
AUndefined
BTrue
CFalse
DNone
💡 Hint
Look at the 'receiver_called' row under 'After Step 4' in the variable tracker.
If you remove the 'created' check inside the receiver, what changes in the execution table?
AReceiver prints message on both create and update
BReceiver never runs
CSignal is not sent
DReceiver runs only on update
💡 Hint
Refer to the 'Receiver Action' and 'Output' columns for steps 1 and 2 in the execution table.
Concept Snapshot
Django signals let apps react to events without tight links.
Use signals for side effects like logging or notifications.
Avoid signals for core logic or when explicit calls are clearer.
Signals send events; receivers listen and act.
Check event details (like 'created') inside receivers.
Too many signals can make debugging hard.
Full Transcript
Django signals are tools to let parts of your app respond when something happens, like saving a model. When an event occurs, Django sends a signal. Receivers that listen to this signal run their code. For example, after saving a new model instance, a receiver can print a message. But signals only work if receivers are connected. If no receiver listens, nothing happens. Also, signals run on many events, so inside the receiver you often check details like if the instance was just created. Signals are good for side effects like sending emails or logging, but avoid using them for main logic because they can make your code harder to understand and debug.