0
0
Djangoframework~20 mins

Connecting signal handlers in Django - Practice Problems & Coding Challenges

Choose your learning style9 modes available
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 signal handler is connected incorrectly?
Consider this Django signal connection code:
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?
AThe signal handler will be called for all models, ignoring the sender filter.
BDjango will raise an ImportError at runtime when connecting the signal.
CThe signal handler will be called twice for each save event.
DThe signal handler will never be called because the sender is not recognized.
Attempts:
2 left
💡 Hint
Think about how the sender argument filters which model triggers the signal.
📝 Syntax
intermediate
1: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?
Apost_delete.connect(my_handler, sender=MyModel)
Bpost_delete.connect(sender=MyModel, my_handler)
Cpost_delete.connect(my_handler)
Dpost_delete.connect(my_handler, MyModel)
Attempts:
2 left
💡 Hint
Remember the order of arguments for the connect method.
state_output
advanced
2:00remaining
What is printed when a signal handler modifies instance data before saving?
Given this code:
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?
Ahello
BHELLO
CNone
DAn error is raised
Attempts:
2 left
💡 Hint
The pre_save signal runs before the model instance is saved.
🔧 Debug
advanced
2:30remaining
Why does this signal handler run multiple times unexpectedly?
This code connects a signal handler inside a Django view function:
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?
ABecause the signal connection happens on every request, adding duplicate handlers.
BBecause post_save signals are only sent once per model class.
CBecause the handler function is defined inside the view, it is not recognized.
DBecause Django caches signal handlers and reuses them.
Attempts:
2 left
💡 Hint
Think about where the connect call is placed and how often the view runs.
🧠 Conceptual
expert
3: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?
AFilters signals to only those with matching dispatch_uid.
BChanges the order in which signal handlers are called.
CPrevents the same signal handler from being connected multiple times.
DMakes the signal handler run asynchronously.
Attempts:
2 left
💡 Hint
Think about how Django avoids duplicate signal connections.