0
0
Djangoframework~10 mins

Connecting signal handlers in Django - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Connecting signal handlers
Define signal handler function
Import signal and handler
Connect handler to signal
Trigger signal event
Signal calls handler
Handler executes code
This flow shows how you define a function to handle a signal, connect it, then when the signal triggers, the handler runs.
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, **kwargs):
    print('Saved:', instance)
Defines a handler that prints a message whenever a MyModel instance is saved.
Execution Table
StepActionSignalHandler Connected?Handler Called?Output
1Import post_save and receivernullNoNo
2Define my_handler functionnullNoNo
3Connect my_handler to post_save for MyModelpost_saveYesNo
4Create and save MyModel instancepost_saveYesYesSaved: <MyModel instance>
5Save another MyModel instancepost_saveYesYesSaved: <MyModel instance>
6No more savespost_saveYesNo
💡 No more model saves, so signal not triggered and handler not called.
Variable Tracker
VariableStartAfter Step 3After Step 4After Step 5Final
handler_connectedfalsetruetruetruetrue
handler_calledfalsefalsetruetruefalse
outputSaved: <MyModel instance>Saved: <MyModel instance> Saved: <MyModel instance>Saved: <MyModel instance> Saved: <MyModel instance>
Key Moments - 3 Insights
Why doesn't the handler run before connecting it to the signal?
Because in the execution_table at Step 3, the handler_connected changes from No to Yes. Before that, the signal has no handler to call.
What triggers the handler to run?
Saving a MyModel instance triggers the post_save signal, which calls the connected handler as shown in Steps 4 and 5.
Can the handler run if the signal is not connected?
No, the handler only runs if connected. The execution_table shows no output before connection at Step 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, at which step is the handler first connected to the signal?
AStep 1
BStep 3
CStep 4
DStep 2
💡 Hint
Check the 'Handler Connected?' column in the execution_table.
At which step does the handler first get called and produce output?
AStep 4
BStep 3
CStep 2
DStep 1
💡 Hint
Look at the 'Handler Called?' and 'Output' columns in the execution_table.
If no MyModel instance is saved, what happens to the handler_called variable?
AIt becomes true
BIt toggles between true and false
CIt stays false
DIt causes an error
💡 Hint
Refer to variable_tracker for 'handler_called' values when no save occurs.
Concept Snapshot
Connecting signal handlers in Django:
1. Define a handler function.
2. Use @receiver decorator or connect() to link it to a signal.
3. When the signal triggers (e.g., model saved), handler runs.
4. Handlers receive sender, instance, and kwargs.
5. Connect before triggering signal to ensure handler runs.
Full Transcript
In Django, connecting signal handlers means writing a function that runs when something happens, like saving a model. First, you import the signal and the receiver decorator. Then, you define your handler function. Next, you connect the handler to the signal using @receiver or connect(). When the event happens, like saving a model instance, Django triggers the signal and calls your handler. The handler can then do things like print a message or update data. The execution table shows the steps: importing, defining, connecting, triggering, and handling. Variables track if the handler is connected and called. Beginners often wonder why the handler doesn't run before connecting; it only runs after connection and signal trigger. If no event happens, the handler never runs. This process helps keep code organized and reactive to events.