Bird
0
0

What is wrong with this signal usage?

medium📝 Debug Q14 of 15
Django - Signals
What is wrong with this signal usage?
from django.db.models.signals import post_save

post_save.connect(handle_save)

def handle_save(sender, instance, **kwargs):
    print('Saved!')
Apost_save signal cannot be connected manually
BMissing sender argument in connect call
CSignal handlers must return a value
DThe signal handler is connected before it is defined
Step-by-Step Solution
Solution:
  1. Step 1: Check order of function definition and connection

    The handler function handle_save is connected before it is defined, causing a NameError.
  2. Step 2: Understand Python execution order

    Python reads top to bottom, so handle_save must be defined before connecting it.
  3. Final Answer:

    The signal handler is connected before it is defined -> Option D
  4. Quick Check:

    Define handler before connecting signal [OK]
Quick Trick: Define handler before connecting signals [OK]
Common Mistakes:
MISTAKES
  • Ignoring function order
  • Assuming sender is always required
  • Thinking signal handlers must return values

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Django Quizzes