Bird
Raised Fist0
Djangoframework~20 mins

Signal dispatch process in Django - Practice Problems & Coding Challenges

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
Challenge - 5 Problems
🎖️
Signal Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when a Django signal is sent?
Consider a Django signal connected to multiple receivers. What is the order in which receivers are called when the signal is sent?
AReceivers are called randomly without any guaranteed order.
BReceivers are called in reverse order of connection.
CReceivers are called in alphabetical order of their function names.
DReceivers are called in the order they were connected to the signal.
Attempts:
2 left
💡 Hint
Think about how Django stores signal receivers internally.
📝 Syntax
intermediate
2:00remaining
Identify the correct way to connect a receiver to a Django signal
Which of the following code snippets correctly connects a receiver function to Django's post_save signal?
Django
from django.db.models.signals import post_save
from django.dispatch import receiver
from myapp.models import MyModel

# Receiver function
@receiver(post_save, sender=MyModel)
def my_handler(sender, instance, created, **kwargs):
    pass
A
post_save.connect(my_handler)
def my_handler(sender, instance, created, **kwargs): pass
B
post_save.connect(my_handler, sender=MyModel)
def my_handler(sender, instance, created, **kwargs): pass
C
@receiver(post_save, sender=MyModel)
def my_handler(sender, instance, created, **kwargs): pass
D
@receiver(post_save)
def my_handler(sender, instance, created, **kwargs): pass
Attempts:
2 left
💡 Hint
Look for the decorator that specifies the sender.
🔧 Debug
advanced
2:00remaining
Why does this Django signal receiver not get called?
Given the following code, why does the receiver function never execute when a MyModel instance is saved?
Django
from django.db.models.signals import post_save
from django.dispatch import receiver
from myapp.models import MyModel

@receiver(post_save, sender=object)
def my_handler(sender, instance, created, **kwargs):
    print('Signal received')

# Somewhere else in the code
obj = MyModel()
obj.save()
AThe receiver is not connected to the specific sender MyModel, so it won't be called.
BThe signal post_save is not imported correctly.
CThe print statement inside the receiver is invalid in Django signals.
DThe receiver function must be connected manually using connect() method.
Attempts:
2 left
💡 Hint
Check if the receiver is listening to the right sender.
state_output
advanced
2:00remaining
What is the output when multiple receivers modify the same instance?
If two receivers connected to the post_save signal modify the same model instance's attribute, what will be the final value after saving?
Django
from django.db.models.signals import post_save
from django.dispatch import receiver
from myapp.models import MyModel

@receiver(post_save, sender=MyModel)
def receiver_one(sender, instance, **kwargs):
    instance.name = 'First'
    instance.save()

@receiver(post_save, sender=MyModel)
def receiver_two(sender, instance, **kwargs):
    instance.name = 'Second'
    instance.save()

obj = MyModel(name='Original')
obj.save()
print(MyModel.objects.get(pk=obj.pk).name)
ARaises a RuntimeError due to recursive save calls
BSecond
COriginal
DFirst
Attempts:
2 left
💡 Hint
Think about what happens when save() is called inside a post_save receiver.
🧠 Conceptual
expert
2:00remaining
How does Django ensure thread safety in signal dispatch?
Django signals can be used in multi-threaded environments. How does Django ensure that signal receivers are called safely without conflicts?
ADjango uses a thread-safe internal lock to serialize signal dispatch calls.
BDjango copies the list of receivers before dispatching to avoid modification during iteration.
CDjango runs each receiver in a separate thread automatically.
DDjango requires developers to manually handle thread safety in signal receivers.
Attempts:
2 left
💡 Hint
Consider how modifying a list during iteration can cause issues.

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