Bird
0
0

Why does this signal handler not run when a Profile instance is saved?

medium📝 Debug Q7 of 15
Django - Signals
Why does this signal handler not run when a Profile instance is saved?
from django.db.models.signals import post_save
from django.dispatch import receiver

@receiver(post_save, sender=User)
def create_profile(sender, instance, created, **kwargs):
    if created:
        Profile.objects.create(user=instance)
ASignal is connected to User, not Profile model
BHandler function is missing the sender argument
Cpost_save signal does not work with Profile model
DProfile model does not have a create method
Step-by-Step Solution
Solution:
  1. Step 1: Analyze the signal connection

    The handler listens to post_save for the User model only.
  2. Step 2: Reason why it doesn't run on Profile save

    Since the signal is not connected to Profile, saving a Profile instance won't trigger this handler.
  3. Final Answer:

    Signal is connected to User, not Profile model -> Option A
  4. Quick Check:

    Signal sender must match model to trigger handler [OK]
Quick Trick: Signal sender must match model instance saved [OK]
Common Mistakes:
MISTAKES
  • Assuming handler runs for all models
  • Confusing handler parameters with sender
  • Thinking post_save doesn't work with Profile

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Django Quizzes