Bird
0
0

What is wrong with this code that tries to use pre_delete signal?

medium📝 Debug Q14 of 15
Django - Signals
What is wrong with this code that tries to use pre_delete signal?
from django.db.models.signals import pre_delete

@pre_delete(sender=Author)
def cleanup(sender, instance, **kwargs):
    print('Cleaning up author data')
AUsing @pre_delete decorator is incorrect; should use @receiver instead.
BMissing import for Author model.
CSignal handler must return a value.
DThe function name 'cleanup' is reserved and cannot be used.
Step-by-Step Solution
Solution:
  1. Step 1: Check signal handler decoration

    Django signals use the @receiver(signal, sender=Model) decorator, not @pre_delete(sender=Model).
  2. Step 2: Confirm correct decorator usage

    Replace @pre_delete(sender=Author) with @receiver(pre_delete, sender=Author) to fix the error.
  3. Final Answer:

    Using @pre_delete decorator is incorrect; should use @receiver instead. -> Option A
  4. Quick Check:

    Use @receiver for signals, not @pre_delete [OK]
Quick Trick: Use @receiver(signal, sender=Model) to decorate signal handlers [OK]
Common Mistakes:
MISTAKES
  • Using signal name as decorator directly
  • Forgetting to import @receiver
  • Assuming signal handlers must return values

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Django Quizzes