Bird
0
0

How do you properly register a pre_save signal handler for a Django model called Book using the @receiver decorator?

easy📝 Syntax Q3 of 15
Django - Signals
How do you properly register a pre_save signal handler for a Django model called Book using the @receiver decorator?
AUse <code>@receiver(pre_save, sender=Book)</code> above the handler function
BCall <code>pre_save.connect(handler, sender=Book)</code> inside the model class
CDefine <code>Book.pre_save = handler</code> in models.py
DUse <code>@receiver(post_save, sender=Book)</code> above the handler function
Step-by-Step Solution
Solution:
  1. Step 1: Understand the decorator usage

    The @receiver decorator takes the signal and sender as arguments.
  2. Step 2: Match signal and sender

    For pre_save on Book, use @receiver(pre_save, sender=Book).
  3. Step 3: Eliminate incorrect options

    Call pre_save.connect(handler, sender=Book) inside the model class is a method call, not decorator usage; C is invalid syntax; D uses the wrong signal.
  4. Final Answer:

    Use @receiver(pre_save, sender=Book) above the handler function -> Option A
  5. Quick Check:

    Decorator syntax requires signal and sender [OK]
Quick Trick: Use @receiver(signal, sender=Model) decorator [OK]
Common Mistakes:
MISTAKES
  • Using post_save instead of pre_save
  • Calling connect inside model class incorrectly
  • Assigning handler directly to model attribute

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Django Quizzes