Challenge - 5 Problems
Signal Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What happens when a signal handler is connected incorrectly?
Consider this Django signal connection code:
What will happen if you forget to import
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)What will happen if you forget to import
MyModel correctly and connect the signal?Attempts:
2 left
💡 Hint
Think about how the sender argument filters which model triggers the signal.
✗ Incorrect
If the sender is not the correct model class, the signal handler won't be triggered for saves of that model. The handler is connected but never called because the sender filter doesn't match.
📝 Syntax
intermediate1:30remaining
Which option correctly connects a signal handler using the
connect method?You want to connect a function
my_handler to Django's post_delete signal for MyModel. Which code snippet is correct?Attempts:
2 left
💡 Hint
Remember the order of arguments for the connect method.
✗ Incorrect
The connect method takes the receiver function as the first argument and the sender as a keyword argument. Option A follows this pattern correctly.
❓ state_output
advanced2:00remaining
What is printed when a signal handler modifies instance data before saving?
Given this code:
What will be printed?
from django.db.models.signals import pre_save
from django.dispatch import receiver
@receiver(pre_save, sender=MyModel)
def modify_name(sender, instance, **kwargs):
instance.name = instance.name.upper()
obj = MyModel(name='hello')
obj.save()
print(obj.name)What will be printed?
Attempts:
2 left
💡 Hint
The pre_save signal runs before the model instance is saved.
✗ Incorrect
The pre_save signal handler changes the instance's name to uppercase before saving. The saved instance and the variable obj reflect this change, so printing obj.name outputs 'HELLO'.
🔧 Debug
advanced2:30remaining
Why does this signal handler run multiple times unexpectedly?
This code connects a signal handler inside a Django view function:
Why might the handler print multiple times on multiple requests?
def my_view(request):
from django.db.models.signals import post_save
def handler(sender, instance, **kwargs):
print('Saved:', instance)
post_save.connect(handler, sender=MyModel)
# ... rest of view code ...
return HttpResponse('Done')Why might the handler print multiple times on multiple requests?
Attempts:
2 left
💡 Hint
Think about where the connect call is placed and how often the view runs.
✗ Incorrect
Connecting a signal handler inside a view means it runs every time the view is called, adding a new handler each time. This causes multiple prints for one save event.
🧠 Conceptual
expert3:00remaining
What is the effect of using
dispatch_uid when connecting signal handlers?In Django signals, what does providing a unique
dispatch_uid argument to connect do?Attempts:
2 left
💡 Hint
Think about how Django avoids duplicate signal connections.
✗ Incorrect
The dispatch_uid argument uniquely identifies a signal handler connection. If a handler with the same dispatch_uid is connected again, Django ignores it, preventing duplicates.