Bird
0
0

How can you ensure a post_delete signal handler only runs for deletions triggered by a specific user action?

hard📝 Application Q9 of 15
Django - Signals
How can you ensure a post_delete signal handler only runs for deletions triggered by a specific user action?
APass a custom flag via <code>delete()</code> method and check it in the handler
BUse <code>pre_delete</code> instead of <code>post_delete</code>
CFilter instances inside the signal handler by user ID
DSignals cannot be filtered by user action
Step-by-Step Solution
Solution:
  1. Step 1: Understand signal context limitations

    Signals do not automatically know the user who triggered deletion.
  2. Step 2: Use custom flags to pass context

    Override the model's delete() method to accept a flag and pass it via kwargs to the signal handler.
  3. Final Answer:

    Pass a custom flag via delete() method and check it in the handler -> Option A
  4. Quick Check:

    Use custom flags to filter signals [OK]
Quick Trick: Pass flags in delete() to filter signal runs [OK]
Common Mistakes:
MISTAKES
  • Expecting signals to know user automatically
  • Filtering by user inside signal without context
  • Assuming pre_delete solves this

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Django Quizzes