0
0
Djangoframework~20 mins

pre_delete and post_delete signals in Django - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Signal Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2: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()
A2,1,3
B3,2,1
C1,2,3
D1,3,2
Attempts:
2 left
💡 Hint
Think about what happens before and after the actual deletion.
state_output
intermediate
1: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()
A
Preparing to delete Sample
Deleted Sample
B
Deleted Sample
Preparing to delete Sample
CPreparing to delete Sample
DDeleted Sample
Attempts:
2 left
💡 Hint
Remember the order of pre_delete and post_delete signals.
📝 Syntax
advanced
1: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?
A
@receiver(pre_delete, sender=MyModel)
def handler(sender, instance, **kwargs):
    pass
B
@receiver(pre_delete sender=MyModel)
def handler(sender, instance, **kwargs):
    pass
Cpre_delete.connect(handler, sender=MyModel)
Dpre_delete.connect(handler sender=MyModel)
Attempts:
2 left
💡 Hint
Look carefully at the decorator syntax.
🔧 Debug
advanced
2: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?
AThe signal handler was connected with the wrong sender or not connected at all.
BThe <code>pre_delete</code> signal handler raises an exception preventing deletion.
CThe model's delete() method was overridden but did not call super().delete().
DThe model instance was deleted using a queryset's delete() method, which bypasses signals.
Attempts:
2 left
💡 Hint
Check how the signal handler is connected and the sender argument.
🧠 Conceptual
expert
2: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.
A<code>pre_delete</code> runs only if the instance has related objects; <code>post_delete</code> runs otherwise.
B<code>pre_delete</code> runs after the instance is removed; <code>post_delete</code> runs before deletion starts.
C<code>pre_delete</code> only runs for bulk deletes; <code>post_delete</code> runs for single instance deletes.
D<code>pre_delete</code> runs before the database deletion; <code>post_delete</code> runs after the instance is removed from the database.
Attempts:
2 left
💡 Hint
Think about when each signal is triggered relative to the actual deletion.