Challenge - 5 Problems
Signal Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What happens when a signal is sent in Django using a receiver?
Consider the following Django signal receiver code. What will be printed when the signal is sent?
Django
from django.dispatch import receiver, Signal my_signal = Signal() @receiver(my_signal) def my_receiver(sender, **kwargs): print(f"Signal received from {sender}") my_signal.send(sender='TestSender')
Attempts:
2 left
💡 Hint
Remember that the sender argument is passed as is to the receiver.
✗ Incorrect
The receiver function prints the sender argument passed during signal sending. Here, sender='TestSender' is a string, so it prints exactly that.
📝 Syntax
intermediate2:00remaining
Which option correctly uses the @receiver decorator for a built-in Django signal?
You want to connect a function to Django's post_save signal for a model named MyModel. Which code snippet is correct?
Django
from django.db.models.signals import post_save from django.dispatch import receiver from myapp.models import MyModel # Choose the correct receiver function below:
Attempts:
2 left
💡 Hint
The sender argument must be the model class, not a string or instance.
✗ Incorrect
The sender parameter expects the model class itself, not a string or an instance. Option A correctly passes MyModel class.
🔧 Debug
advanced2:00remaining
Why does this receiver function never get called?
Given this code, the receiver function does not execute when the signal is sent. What is the cause?
Django
from django.dispatch import receiver, Signal my_signal = Signal() @receiver(my_signal) def my_receiver(sender, **kwargs): print('Signal received') # Signal sent before receiver is imported my_signal.send(sender=None)
Attempts:
2 left
💡 Hint
Think about when the receiver function is registered to the signal.
✗ Incorrect
If the signal is sent before the receiver is imported, the receiver is not connected yet and won't be called.
❓ state_output
advanced2:00remaining
What is the output count of signal calls with multiple receivers?
If two receiver functions are connected to the same signal, and the signal is sent once, how many times will the receivers be called?
Django
from django.dispatch import receiver, Signal my_signal = Signal() @receiver(my_signal) def receiver_one(sender, **kwargs): print('Receiver one called') @receiver(my_signal) def receiver_two(sender, **kwargs): print('Receiver two called') my_signal.send(sender=None)
Attempts:
2 left
💡 Hint
Signals can have multiple receivers connected.
✗ Incorrect
Django signals call all connected receivers when sent. Here, both print statements run once.
🧠 Conceptual
expert2:00remaining
What error occurs if a receiver function has a wrong signature?
Consider a receiver function connected to a Django signal but defined without accepting **kwargs. What happens when the signal is sent?
Django
from django.dispatch import receiver, Signal my_signal = Signal() @receiver(my_signal) def my_receiver(sender): print('Signal received') my_signal.send(sender=None, extra='data')
Attempts:
2 left
💡 Hint
Signal sends extra keyword arguments; receiver must accept them.
✗ Incorrect
If the receiver does not accept **kwargs, extra keyword arguments cause a TypeError.