Bird
0
0

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

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

@receiver(post_save, sender=Book)
def notify(sender, instance, **kwargs):
    print(f"Book saved: {instance.title}")

book = Book(title='Django Basics')
book.save()
ABook saved: None
BError: receiver not connected
CNo output printed
DBook saved: Django Basics
Step-by-Step Solution
Solution:
  1. Step 1: Understand the signal connection

    The @receiver decorator connects notify to post_save for Book.
  2. Step 2: What happens on book.save()?

    Saving the book triggers post_save, calling notify, which prints the book's title.
  3. Final Answer:

    Book saved: Django Basics -> Option D
  4. Quick Check:

    post_save triggers print with instance title = B [OK]
Quick Trick: post_save calls receiver printing instance data [OK]
Common Mistakes:
MISTAKES
  • Assuming no output without explicit call
  • Confusing instance attribute access
  • Thinking receiver needs manual call

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Django Quizzes