Bird
0
0

Given this code snippet, what will be printed when a new User instance is created?

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

@receiver(post_save, sender=User)
def user_created(sender, instance, created, **kwargs):
    if created:
        print(f"User {instance.username} created")
AUser <username> created
BUser created
CNo output
DError: missing parameters
Step-by-Step Solution
Solution:
  1. Step 1: Understand the signal and handler behavior

    The post_save signal triggers after saving a User. The handler prints the username if the instance is newly created.
  2. Step 2: Analyze the print statement

    The print outputs "User {instance.username} created" when created is True, which happens on new instance creation.
  3. Final Answer:

    User <username> created -> Option A
  4. Quick Check:

    post_save with created=True prints username [OK]
Quick Trick: post_save with created=True means new instance [OK]
Common Mistakes:
MISTAKES
  • Assuming print runs on every save
  • Ignoring the created flag
  • Expecting error due to missing parameters

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Django Quizzes