Bird
0
0

You want to send a confirmation email only when a new Order is created, but not when it is updated. How would you use Django signals to achieve this decoupled behavior?

hard📝 Application Q8 of 15
Django - Signals
You want to send a confirmation email only when a new Order is created, but not when it is updated. How would you use Django signals to achieve this decoupled behavior?
AConnect a post_save signal handler that checks if 'created' is True before sending email
BUse pre_save signal and send email unconditionally
CCall the email function directly inside the view handling the order
DOverride the save method of Order to send email always
Step-by-Step Solution
Solution:
  1. Step 1: Identify signal and condition for new creation

    post_save signal provides a created boolean indicating if the instance is new.
  2. Step 2: Use a handler that sends email only if created is True

    This ensures email is sent only on creation, keeping logic decoupled from views or models.
  3. Final Answer:

    Connect a post_save signal handler that checks if 'created' is True before sending email -> Option A
  4. Quick Check:

    Use post_save with created=True to send email once [OK]
Quick Trick: Check 'created' in post_save to act only on new instances [OK]
Common Mistakes:
MISTAKES
  • Using pre_save without condition
  • Calling email in views breaking decoupling
  • Overriding save method unnecessarily

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Django Quizzes