Bird
Raised Fist0
Djangoframework~10 mins

Receiver decorator in Django - Interactive Code Practice

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
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import the receiver decorator from Django signals.

Django
from django.dispatch import [1]
Drag options to blanks, or click blank then click option'
Areceiver
Bsignal
Cconnect
Dsend
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'signal' instead of 'receiver' for import.
Trying to import 'connect' which is a method, not a decorator.
2fill in blank
medium

Complete the code to decorate the function so it listens to the post_save signal.

Django
@receiver([1])
def my_handler(sender, instance, **kwargs):
    pass
Drag options to blanks, or click blank then click option'
Apost_delete
Bpre_save
Cpost_save
Dpre_delete
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'pre_save' which runs before saving.
Confusing delete signals with save signals.
3fill in blank
hard

Fix the error in the decorator to correctly listen to the signal from the User model.

Django
@receiver(post_save, sender=[1])
def user_saved(sender, instance, **kwargs):
    print('User saved')
Drag options to blanks, or click blank then click option'
Aauth.User
Bmodels.User
Cuser
DUser
Attempts:
3 left
💡 Hint
Common Mistakes
Using a string like 'auth.User' instead of the User class.
Using lowercase 'user' which is undefined.
4fill in blank
hard

Fill both blanks to connect a function to the pre_delete signal for the Article model.

Django
@receiver([1], sender=[2])
def article_deleted(sender, instance, **kwargs):
    print('Article deleted')
Drag options to blanks, or click blank then click option'
Apre_delete
Bpost_save
CArticle
DUser
Attempts:
3 left
💡 Hint
Common Mistakes
Using post_save signal which is unrelated to deletion.
Using User model instead of Article.
5fill in blank
hard

Fill all three blanks to create a receiver for the m2m_changed signal on the tags field of the Post model.

Django
@receiver([1], sender=[2].tags.through)
def tags_changed(sender, instance, action, **kwargs):
    if action == [3]:
        print('Tags updated')
Drag options to blanks, or click blank then click option'
Am2m_changed
BPost
C'post_add'
Dpost_save
Attempts:
3 left
💡 Hint
Common Mistakes
Using post_save signal which does not track m2m changes.
Using 'pre_add' instead of 'post_add' for action.
Using wrong sender model.

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