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 Django model instance is deleted?
Consider a Django model with connected
pre_delete and post_delete signals. What is the correct order of signal execution when an instance is deleted?Django
from django.db.models.signals import pre_delete, post_delete from django.dispatch import receiver from myapp.models import MyModel @receiver(pre_delete, sender=MyModel) def before_delete(sender, instance, **kwargs): print('Before delete') @receiver(post_delete, sender=MyModel) def after_delete(sender, instance, **kwargs): print('After delete') obj = MyModel.objects.create(name='test') obj.delete()
Attempts:
2 left
💡 Hint
Think about what happens before and after the actual deletion.
✗ Incorrect
The pre_delete signal runs before the model instance is deleted from the database. Then the instance is deleted. Finally, the post_delete signal runs after deletion.
❓ state_output
intermediate1:30remaining
What is printed by the signal handlers on deleting a model instance?
Given the following signal handlers connected to a Django model, what will be printed when an instance is deleted?
Django
from django.db.models.signals import pre_delete, post_delete from django.dispatch import receiver from myapp.models import MyModel @receiver(pre_delete, sender=MyModel) def before_delete(sender, instance, **kwargs): print(f'Preparing to delete {instance.name}') @receiver(post_delete, sender=MyModel) def after_delete(sender, instance, **kwargs): print(f'Deleted {instance.name}') obj = MyModel.objects.create(name='Sample') obj.delete()
Attempts:
2 left
💡 Hint
Remember the order of pre_delete and post_delete signals.
✗ Incorrect
The pre_delete handler prints before deletion, and the post_delete handler prints after deletion. Both messages appear in that order.
📝 Syntax
advanced1:30remaining
Identify the syntax error in connecting a pre_delete signal
Which option contains a syntax error when connecting a
pre_delete signal to a Django model?Attempts:
2 left
💡 Hint
Look carefully at the decorator syntax.
✗ Incorrect
Option B is missing a comma between pre_delete and sender=MyModel in the decorator, causing a syntax error.
🔧 Debug
advanced2:00remaining
Why does the post_delete signal not run after deleting a model instance?
A developer notices that the
post_delete signal handler is never called after deleting a model instance. Which of the following is the most likely cause?Attempts:
2 left
💡 Hint
Check how the signal handler is connected and the sender argument.
✗ Incorrect
If the signal handler is not connected properly or the sender is incorrect, the post_delete signal will not be triggered.
🧠 Conceptual
expert2:30remaining
What is the main difference between pre_delete and post_delete signals in Django?
Choose the option that best describes the key conceptual difference between
pre_delete and post_delete signals in Django.Attempts:
2 left
💡 Hint
Think about when each signal is triggered relative to the actual deletion.
✗ Incorrect
pre_delete is triggered just before the model instance is deleted from the database. post_delete is triggered immediately after the deletion.