Bird
0
0

Given this code snippet, what will be printed when a new Article instance is saved?

medium📝 component behavior Q4 of 15
Django - Signals
Given this code snippet, what will be printed when a new Article instance is saved?
from django.db.models.signals import pre_save
from django.dispatch import receiver

@receiver(pre_save, sender=Article)
def before_save(sender, instance, **kwargs):
    print('Saving:', instance.title)

article = Article(title='Hello')
article.save()
ASaving: None
BNo output
CError: receiver not connected
DSaving: Hello
Step-by-Step Solution
Solution:
  1. Step 1: Understand the receiver decorator

    The @receiver(pre_save, sender=Article) connects the before_save function to the pre_save signal for Article.
  2. Step 2: Check what happens on save

    When article.save() runs, before_save prints the title 'Hello'.
  3. Final Answer:

    Saving: Hello -> Option D
  4. Quick Check:

    pre_save signal prints title before save [OK]
Quick Trick: pre_save runs before save, prints instance data [OK]
Common Mistakes:
MISTAKES
  • Assuming no output without explicit connect call
  • Thinking print shows None if instance has title
  • Confusing pre_save with post_save output timing

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Django Quizzes