Bird
Raised Fist0
Djangoframework~20 mins

Receiver decorator in Django - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
Signal Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when a signal is sent in Django using a receiver?
Consider the following Django signal receiver code. What will be printed when the signal is sent?
Django
from django.dispatch import receiver, Signal

my_signal = Signal()

@receiver(my_signal)
def my_receiver(sender, **kwargs):
    print(f"Signal received from {sender}")

my_signal.send(sender='TestSender')
ATypeError due to missing arguments
BSignal received from <class 'str'>
CNo output, signal not connected
DSignal received from TestSender
Attempts:
2 left
💡 Hint
Remember that the sender argument is passed as is to the receiver.
📝 Syntax
intermediate
2:00remaining
Which option correctly uses the @receiver decorator for a built-in Django signal?
You want to connect a function to Django's post_save signal for a model named MyModel. Which code snippet is correct?
Django
from django.db.models.signals import post_save
from django.dispatch import receiver
from myapp.models import MyModel

# Choose the correct receiver function below:
A
@receiver(post_save, sender=MyModel)
def my_handler(sender, instance, **kwargs):
    print('Saved')
B
@receiver(post_save)
def my_handler(sender, instance, **kwargs):
    print('Saved')
C
@receiver(post_save, sender=MyModel())
def my_handler(sender, instance, **kwargs):
    print('Saved')
D
@receiver(post_save, sender='MyModel')
def my_handler(sender, instance, **kwargs):
    print('Saved')
Attempts:
2 left
💡 Hint
The sender argument must be the model class, not a string or instance.
🔧 Debug
advanced
2:00remaining
Why does this receiver function never get called?
Given this code, the receiver function does not execute when the signal is sent. What is the cause?
Django
from django.dispatch import receiver, Signal

my_signal = Signal()

@receiver(my_signal)
def my_receiver(sender, **kwargs):
    print('Signal received')

# Signal sent before receiver is imported
my_signal.send(sender=None)
AThe sender argument must not be None
BThe signal object is not instantiated properly
CThe signal is sent before the receiver function is imported and connected
DThe receiver function has incorrect parameters
Attempts:
2 left
💡 Hint
Think about when the receiver function is registered to the signal.
state_output
advanced
2:00remaining
What is the output count of signal calls with multiple receivers?
If two receiver functions are connected to the same signal, and the signal is sent once, how many times will the receivers be called?
Django
from django.dispatch import receiver, Signal

my_signal = Signal()

@receiver(my_signal)
def receiver_one(sender, **kwargs):
    print('Receiver one called')

@receiver(my_signal)
def receiver_two(sender, **kwargs):
    print('Receiver two called')

my_signal.send(sender=None)
AOnly the first receiver is called
BBoth receivers are called once, so two print statements appear
COnly the last receiver connected is called
DNo receivers are called because of signal conflict
Attempts:
2 left
💡 Hint
Signals can have multiple receivers connected.
🧠 Conceptual
expert
2:00remaining
What error occurs if a receiver function has a wrong signature?
Consider a receiver function connected to a Django signal but defined without accepting **kwargs. What happens when the signal is sent?
Django
from django.dispatch import receiver, Signal

my_signal = Signal()

@receiver(my_signal)
def my_receiver(sender):
    print('Signal received')

my_signal.send(sender=None, extra='data')
ATypeError: my_receiver() got an unexpected keyword argument 'extra'
BSignal received is printed without error
CAttributeError due to missing kwargs
DNo output, receiver silently ignored
Attempts:
2 left
💡 Hint
Signal sends extra keyword arguments; receiver must accept them.

Practice

(1/5)
1. What is the main purpose of the @receiver decorator in Django?
easy
A. To register a template filter
B. To create a new database model
C. To connect a function to a signal so it runs automatically when the signal is sent
D. To define a URL route in Django

Solution

  1. Step 1: Understand what signals do in Django

    Signals notify parts of your app when something happens, like saving a model.
  2. Step 2: Role of the @receiver decorator

    The decorator links a function to a signal so it runs automatically when the signal is sent.
  3. Final Answer:

    To connect a function to a signal so it runs automatically when the signal is sent -> Option C
  4. Quick Check:

    Receiver decorator connects functions to signals = D [OK]
Hint: Receiver links functions to signals for automatic execution [OK]
Common Mistakes:
  • Confusing receiver with model or URL definitions
  • Thinking receiver creates models or templates
  • Mixing up signals with URL routing
2. Which of the following is the correct syntax to use the @receiver decorator for the post_save signal of a model named Book?
easy
A. @receiver(post_save, sender=Book) def my_handler(sender, instance, **kwargs): pass
B. @receiver(post_save(Book)) def my_handler(sender, instance, **kwargs): pass
C. @receiver(sender=Book, post_save) def my_handler(sender, instance, **kwargs): pass
D. @receiver(post_save) def my_handler(Book, instance, **kwargs): pass

Solution

  1. Step 1: Check the correct decorator syntax

    The @receiver decorator takes the signal as first argument and sender=ModelName as keyword argument.
  2. Step 2: Match the correct function signature

    The handler function must accept sender, instance, and **kwargs.
  3. Final Answer:

    @receiver(post_save, sender=Book) def my_handler(sender, instance, **kwargs): pass -> Option A
  4. Quick Check:

    Correct decorator syntax = C [OK]
Hint: Use @receiver(signal, sender=Model) syntax [OK]
Common Mistakes:
  • Passing sender as positional argument
  • Incorrect function parameters
  • Missing sender keyword argument
3. Given this code snippet, what will be printed when a Book instance is saved?
from django.db.models.signals import post_save
from django.dispatch import receiver

@receiver(post_save, sender=Book)
def notify(sender, instance, **kwargs):
    print(f"Book saved: {instance.title}")

book = Book(title='Django Basics')
book.save()
medium
A. Book saved: None
B. Error: receiver not connected
C. No output printed
D. Book saved: Django Basics

Solution

  1. Step 1: Understand the signal connection

    The @receiver decorator connects notify to post_save for Book.
  2. Step 2: What happens on book.save()?

    Saving the book triggers post_save, calling notify, which prints the book's title.
  3. Final Answer:

    Book saved: Django Basics -> Option D
  4. Quick Check:

    post_save triggers print with instance title = B [OK]
Hint: post_save calls receiver printing instance data [OK]
Common Mistakes:
  • Assuming no output without explicit call
  • Confusing instance attribute access
  • Thinking receiver needs manual call
4. Identify the error in this signal receiver code:
from django.db.models.signals import post_save
from django.dispatch import receiver

@receiver(post_save, sender=Author)
def handle_save(instance, **kwargs):
    print(f"Author saved: {instance.name}")
medium
A. Incorrect decorator usage, should not use sender
B. Missing sender parameter in function definition
C. Function name must be post_save_handler
D. No error, code is correct

Solution

  1. Step 1: Check function parameters for signal handlers

    Signal handlers must accept sender, instance, and **kwargs.
  2. Step 2: Identify missing parameter

    The function handle_save lacks the sender parameter, causing an error.
  3. Final Answer:

    Missing sender parameter in function definition -> Option B
  4. Quick Check:

    Signal handler must have sender parameter = A [OK]
Hint: Signal handlers need sender, instance, **kwargs parameters [OK]
Common Mistakes:
  • Omitting sender parameter
  • Wrong function signature order
  • Assuming function name matters
5. You want to run a function only when a User model instance is created (not updated). How do you use the @receiver decorator with post_save to achieve this?
hard
A. @receiver(post_save, sender=User) def user_created(sender, instance, created, **kwargs): if created: print('New user created')
B. @receiver(post_save, sender=User) def user_created(sender, instance, **kwargs): print('New user created')
C. @receiver(post_save, sender=User) def user_created(sender, instance, created=False, **kwargs): if created == False: print('New user created')
D. @receiver(post_save, sender=User) def user_created(sender, instance, **kwargs): if not created: print('New user created')

Solution

  1. Step 1: Understand the created flag in post_save

    The post_save signal passes a created boolean indicating if the instance was newly created.
  2. Step 2: Use created to run code only on creation

    Check if created: inside the receiver function to run code only when a new instance is created.
  3. Final Answer:

    @receiver(post_save, sender=User) def user_created(sender, instance, created, **kwargs): if created: print('New user created') -> Option A
  4. Quick Check:

    Use created flag in post_save receiver = A [OK]
Hint: Check 'created' flag in post_save receiver to detect new instances [OK]
Common Mistakes:
  • Ignoring the created parameter
  • Checking created incorrectly
  • Missing created parameter in function