Bird
Raised Fist0
Djangoframework~10 mins

Signal dispatch process in Django - Step-by-Step Execution

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Concept Flow - Signal dispatch process
Signal sent
Dispatcher receives signal
Find connected receivers
For each receiver
Call receiver function
Receiver processes signal
Signal dispatch complete
This flow shows how Django sends a signal, finds all connected receivers, calls each one, and completes the dispatch.
Execution Sample
Django
from django.dispatch import Signal

my_signal = Signal()

receiver_called = False

def receiver(sender, **kwargs):
    global receiver_called
    receiver_called = True
    print('Signal received')

my_signal.connect(receiver)
my_signal.send(sender=None)
This code creates a signal, connects a receiver function, and sends the signal to trigger the receiver.
Execution Table
StepActionEvaluationResult
1Create signal objectmy_signal = Signal()Signal object created
2Define receiver functiondef receiver(...)Function ready
3Connect receivermy_signal.connect(receiver)Receiver added to signal
4Send signalmy_signal.send(sender=None)Dispatcher starts
5Find receiversReceivers list contains 'receiver'One receiver found
6Call receiverreceiver(sender=None)Prints 'Signal received'
7Finish dispatchNo more receiversSignal dispatch complete
💡 All connected receivers called, signal dispatch ends
Variable Tracker
VariableStartAfter Step 3After Step 4After Step 6Final
my_signal.receivers[][receiver][receiver][receiver][receiver]
receiver_calledFalseFalseFalseTrueTrue
Key Moments - 2 Insights
Why does the receiver function get called when we send the signal?
Because in step 5 the dispatcher finds the connected receiver, and in step 6 it calls the receiver function as shown in the execution_table.
What happens if no receivers are connected when the signal is sent?
The dispatcher finds an empty receivers list at step 5, so no functions are called and the dispatch ends immediately as per the exit_note.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the state of my_signal.receivers after step 3?
ANone
BEmpty list
CContains the receiver function
DContains multiple receivers
💡 Hint
Check variable_tracker column 'After Step 3' for my_signal.receivers
At which step does the receiver function actually get called?
AStep 4
BStep 6
CStep 5
DStep 7
💡 Hint
Look at execution_table row with action 'Call receiver'
If we connect two receivers before sending, how would the execution_table change?
AStep 5 would find two receivers, and step 6 would call each one
BStep 5 would find one receiver, step 6 calls only one
CStep 4 would not send the signal
DNo change in execution_table
💡 Hint
Consider how the dispatcher finds and calls all connected receivers
Concept Snapshot
Django signals let parts of your app talk to each other.
Create a Signal object.
Connect receiver functions to it.
Send the signal to call all receivers.
Receivers run their code when signal is sent.
Signal dispatch calls each connected receiver in order.
Full Transcript
In Django, the signal dispatch process starts when a signal is sent. The dispatcher looks up all functions connected as receivers to that signal. It then calls each receiver function one by one, passing sender and any extra info. After all receivers run, the dispatch ends. This lets different parts of your app respond to events without tight connections. The example code shows creating a signal, connecting a receiver, and sending the signal to trigger the receiver. The execution table traces each step from signal creation to calling the receiver. Variables track the list of receivers and if the receiver was called. Key moments clarify why receivers get called and what happens if none are connected. The quiz tests understanding of when receivers are found and called, and how multiple receivers affect dispatch. The snapshot summarizes the process simply for quick recall.

Practice

(1/5)
1. What is the main purpose of Django signals in the signal dispatch process?
easy
A. To allow different parts of an app to communicate automatically when events happen
B. To store data in the database
C. To create user interface elements
D. To handle HTTP requests directly

Solution

  1. Step 1: Understand what signals do

    Django signals send messages between parts of an app when something happens.
  2. Step 2: Identify the purpose of signals

    Signals let parts of the app react automatically to events like saving or deleting data.
  3. Final Answer:

    To allow different parts of an app to communicate automatically when events happen -> Option A
  4. Quick Check:

    Signals enable automatic communication [OK]
Hint: Signals connect events to actions automatically [OK]
Common Mistakes:
  • Thinking signals store data
  • Confusing signals with UI elements
  • Believing signals handle HTTP requests
2. Which of the following is the correct way to connect a receiver function to a Django signal?
easy
A. signal.connect(receiver_function, sender=ModelClass)
B. receiver_function.connect(signal, sender=ModelClass)
C. signal.send(receiver_function, sender=ModelClass)
D. receiver_function.send(signal, sender=ModelClass)

Solution

  1. Step 1: Recall the syntax for connecting signals

    In Django, you connect a receiver to a signal using signal.connect(receiver, sender=...).
  2. Step 2: Match the correct method call

    signal.connect(receiver_function, sender=ModelClass) uses signal.connect with the receiver function and sender, which is correct.
  3. Final Answer:

    signal.connect(receiver_function, sender=ModelClass) -> Option A
  4. Quick Check:

    Use signal.connect to attach receivers [OK]
Hint: Remember: signal.connect(receiver, sender) [OK]
Common Mistakes:
  • Calling connect on the receiver instead of the signal
  • Using send instead of connect to attach receivers
  • Mixing up sender and receiver parameters
3. Given this code snippet, what will be printed when a new User instance is saved?
from django.db.models.signals import post_save
from django.dispatch import receiver

@receiver(post_save, sender=User)
def user_saved(sender, instance, created, **kwargs):
    if created:
        print(f"User {instance.username} created")
    else:
        print(f"User {instance.username} updated")

user = User(username='alice')
user.save()
medium
A. User alice updated
B. User alice created
C. No output
D. Error: receiver not connected

Solution

  1. Step 1: Understand the post_save signal and receiver

    The receiver listens for post_save on User. It prints 'created' if the instance is new.
  2. Step 2: Analyze the save call

    Creating a new User and calling save triggers post_save with created=True, so it prints 'User alice created'.
  3. Final Answer:

    User alice created -> Option B
  4. Quick Check:

    New save triggers created=True message [OK]
Hint: post_save with created=True means new instance saved [OK]
Common Mistakes:
  • Assuming updated message prints on new save
  • Thinking receiver is not connected automatically
  • Ignoring the created flag in the receiver
4. What is wrong with this signal receiver code when trying to target deletes from a specific model only?
from django.db.models.signals import pre_delete

def my_receiver(sender, instance, **kwargs):
    print(f"Deleting {instance}")

pre_delete.connect(my_receiver)
medium
A. Signal pre_delete does not exist
B. Receiver function must be decorated with @receiver
C. Missing sender argument in connect call
D. Receiver function must return a value

Solution

  1. Step 1: Check the connect call parameters

    The connect call lacks the sender argument, so it connects to all senders.
  2. Step 2: Identify why it doesn't target a specific model

    Without specifying sender, the receiver runs for deletes on any model. Adding sender=ModelClass limits it to that model.
  3. Final Answer:

    Missing sender argument in connect call -> Option C
  4. Quick Check:

    Always specify sender for model-specific receivers [OK]
Hint: Always specify sender in connect unless you want all senders [OK]
Common Mistakes:
  • Thinking @receiver decorator is required
  • Believing pre_delete signal does not exist
  • Expecting receiver to return a value
5. You want to send a custom signal when a blog post is published. Which steps correctly describe the signal dispatch process to achieve this?
hard
A. Override the save method without using signals, then print a message
B. Call the receiver function directly without connecting it to a signal
C. Use Django's built-in signals only; custom signals are not supported
D. Define a custom Signal, connect a receiver with @receiver decorator, then send the signal when publishing

Solution

  1. Step 1: Define a custom Signal

    Create a Signal instance to represent the blog post published event.
  2. Step 2: Connect a receiver function

    Use the @receiver decorator or signal.connect to attach a function that reacts to the signal.
  3. Step 3: Send the signal when publishing

    Call signal.send(sender=..., instance=...) at the point where the blog post is published.
  4. Final Answer:

    Define a custom Signal, connect a receiver with @receiver decorator, then send the signal when publishing -> Option D
  5. Quick Check:

    Custom signal + receiver + send = correct process [OK]
Hint: Custom signals need definition, connection, and sending [OK]
Common Mistakes:
  • Trying to use only built-in signals for custom events
  • Calling receiver directly without signal
  • Overriding save without signals when signals are needed