0
0
Djangoframework~20 mins

Signal dispatch process 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 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.