Bird
0
0

Which of the following is the correct way to use the @receiver decorator to listen to the post_save signal for a Django model named Article?

easy📝 Syntax Q3 of 15
Django - Signals
Which of the following is the correct way to use the @receiver decorator to listen to the post_save signal for a Django model named Article?
A@receiver(post_save, sender=Article) def article_saved(sender, instance, **kwargs): pass
B@receiver(post_save) def article_saved(sender, instance, **kwargs): pass
C@receiver(Article.post_save) def article_saved(sender, instance, **kwargs): pass
D@receiver(post_save, model=Article) def article_saved(sender, instance, **kwargs): pass
Step-by-Step Solution
Solution:
  1. Step 1: Identify signal and sender

    The post_save signal is imported from django.db.models.signals, and the sender is the model class.
  2. Step 2: Correct syntax for @receiver

    The decorator requires the signal and the sender keyword argument to specify which model's signal to listen to.
  3. Final Answer:

    @receiver(post_save, sender=Article) is the correct syntax. -> Option A
  4. Quick Check:

    Signal and sender must be specified [OK]
Quick Trick: Use sender=ModelClass with post_save in @receiver [OK]
Common Mistakes:
MISTAKES
  • Omitting sender argument
  • Using incorrect keyword like model instead of sender
  • Referencing signal as an attribute of the model

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Django Quizzes