Bird
0
0

Consider this code snippet:

medium📝 component behavior Q5 of 15
Django - Signals
Consider this code snippet:
from django.db.models.signals import pre_delete
from django.dispatch import receiver

@receiver(pre_delete, sender=Article)
def cleanup(sender, instance, **kwargs):
    print(f"Cleaning up {instance.title}")

What will happen when an Article instance is deleted?
ANo message will print because pre_delete is incorrect
BThe message will print after deletion
CThe message 'Cleaning up [title]' will print before deletion
DAn error will occur because signals can't print
Step-by-Step Solution
Solution:
  1. Step 1: Understand pre_delete signal timing

    The pre_delete signal fires before the model instance is deleted from the database.
  2. Step 2: Analyze the handler behavior

    The handler prints a message using the instance's title before deletion.
  3. Final Answer:

    The message 'Cleaning up [title]' will print before deletion -> Option C
  4. Quick Check:

    pre_delete runs before delete = A [OK]
Quick Trick: pre_delete runs before deletion, post_delete after [OK]
Common Mistakes:
MISTAKES
  • Confusing pre_delete with post_delete
  • Thinking signals can't perform print or logging
  • Assuming no output from signals

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Django Quizzes