Bird
Raised Fist0
Djangoframework~20 mins

When signals are appropriate vs not in Django - Practice Questions

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
🎖️
Django Signals Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
When should Django signals be used?

Which scenario is the best use case for Django signals?

AWriting raw SQL queries to update related tables.
BDirectly querying the database for user data in a view.
CAutomatically updating a user's profile when the user model is saved.
DManually calling a function after saving a model in the same view.
Attempts:
2 left
💡 Hint

Think about when you want to react to changes without changing the original code flow.

component_behavior
intermediate
2:00remaining
What happens if a signal handler raises an exception?

Consider a Django signal handler connected to the post_save signal. What happens if the handler raises an exception?

AThe exception stops the save operation and propagates up, possibly causing a server error.
BThe exception is silently ignored and the save operation completes normally.
CThe signal handler retries automatically until it succeeds.
DThe signal handler logs the error and continues without affecting the save.
Attempts:
2 left
💡 Hint

Think about how exceptions affect the flow of code execution.

📝 Syntax
advanced
2:00remaining
Identify the correct way to connect a Django signal

Which code snippet correctly connects a post_save signal to a handler function?

Django
from django.db.models.signals import post_save
from django.dispatch import receiver
from myapp.models import MyModel

# Handler function here
A
@receiver(post_save, sender=MyModel)
def my_handler(sender, instance, **kwargs):
    print('Saved!')
B
post_save.connect(my_handler)
def my_handler(sender, instance, **kwargs):
    print('Saved!')
C
@receiver(post_save)
def my_handler(sender, instance, **kwargs):
    print('Saved!')
D
post_save.connect(my_handler, sender=MyModel)
def my_handler(sender, instance):
    print('Saved!')
Attempts:
2 left
💡 Hint

Remember the decorator syntax and required parameters for signal handlers.

🔧 Debug
advanced
2:00remaining
Why is a Django signal handler not called?

Given this code, why might the my_handler function not be called after saving MyModel?

Django
from django.db.models.signals import post_save
from django.dispatch import receiver
from myapp.models import MyModel

@receiver(post_save, sender=MyModel)
def my_handler(sender, instance, **kwargs):
    print('Saved!')

# In another file
obj = MyModel()
obj.save()
AThe signal handler function has the wrong parameters and raises an error silently.
BThe signal handler file is not imported anywhere, so the connection never happens.
CThe model instance is not saved properly because save() is missing parentheses.
DThe post_save signal does not work with MyModel instances.
Attempts:
2 left
💡 Hint

Think about how Django loads signal handlers and when connections are made.

🧠 Conceptual
expert
3:00remaining
When are Django signals NOT appropriate?

Which situation is NOT a good use case for Django signals?

AUpdating a cache when a related model changes.
BSending a notification email immediately after a user registers.
CLogging model changes for audit purposes automatically.
DPerforming complex business logic that requires multiple database queries and transactions.
Attempts:
2 left
💡 Hint

Consider the complexity and side effects of the task inside a signal handler.

Practice

(1/5)
1. Which situation is best suited for using Django signals?
easy
A. Sending an email notification after a user registers
B. Performing complex data validation before saving a model
C. Replacing a model's save method with custom logic
D. Directly calling a function from a view to update related data

Solution

  1. Step 1: Understand signal purpose

    Django signals are designed to react automatically to events like model saves without changing the model code.
  2. Step 2: Match use case to signals

    Sending an email notification after user registration is a small side task that fits well with signals.
  3. Final Answer:

    Sending an email notification after a user registers -> Option A
  4. Quick Check:

    Signals = small side tasks [OK]
Hint: Use signals for small automatic reactions, not complex logic [OK]
Common Mistakes:
  • Using signals for complex validation
  • Overriding model methods instead of signals
  • Calling functions directly when signals fit better
2. Which of the following is the correct way to connect a Django signal?
easy
A. post_save.connect(my_handler, sender=MyModel)
B. MyModel.post_save(my_handler)
C. connect_signal(post_save, my_handler, MyModel)
D. signal.connect(post_save, MyModel, my_handler)

Solution

  1. Step 1: Recall Django signal syntax

    The correct syntax to connect a signal is using the signal's connect method with the handler and sender model.
  2. Step 2: Identify correct syntax

    post_save.connect(my_handler, sender=MyModel) matches Django's documented pattern.
  3. Final Answer:

    post_save.connect(my_handler, sender=MyModel) -> Option A
  4. Quick Check:

    Signal connect syntax = post_save.connect(my_handler, sender=MyModel) [OK]
Hint: Remember: signal.connect(handler, sender=Model) [OK]
Common Mistakes:
  • Swapping argument order
  • Calling signal as a method on model
  • Using undefined connect_signal function
3. Given this code snippet, what will happen when a new Book instance is saved?
from django.db.models.signals import post_save
from django.dispatch import receiver

@receiver(post_save, sender=Book)
def notify_author(sender, instance, created, **kwargs):
    if created:
        print(f"Notify {instance.author} about new book")

book = Book(title='Django Tips', author='Alice')
book.save()
medium
A. Raises an error because of missing signal connection
B. Prints 'Notify Alice about new book'
C. Does nothing because the signal is not connected
D. Prints nothing because created is always False

Solution

  1. Step 1: Understand signal decorator usage

    The @receiver decorator connects the notify_author function to post_save for Book automatically.
  2. Step 2: Check signal behavior on save

    When a new Book instance is saved, created=True, so the print statement runs with author 'Alice'.
  3. Final Answer:

    Prints 'Notify Alice about new book' -> Option B
  4. Quick Check:

    post_save with created=True triggers print [OK]
Hint: @receiver auto-connects signals; created=True means new instance [OK]
Common Mistakes:
  • Assuming signals need manual connect with @receiver
  • Thinking created is False on new save
  • Ignoring the print inside the signal handler
4. 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!')
medium
A. post_save signal cannot be connected manually
B. Missing sender argument in connect call
C. Signal handlers must return a value
D. The signal handler is connected before it is defined

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]
Hint: Define handler before connecting signals [OK]
Common Mistakes:
  • Ignoring function order
  • Assuming sender is always required
  • Thinking signal handlers must return values
5. You want to update a user's profile data immediately after the user is created, but the update requires complex logic involving multiple models and external API calls. What is the best approach?
hard
A. Use a Django signal to handle the profile update automatically
B. Override the User model's save method to include the update logic
C. Call a dedicated function directly after user creation in the view
D. Use a post_save signal but put all complex logic inside it

Solution

  1. Step 1: Evaluate complexity and clarity

    Complex logic with multiple models and external calls is better handled explicitly for clarity and error handling.
  2. Step 2: Choose direct call over signals for complex tasks

    Calling a dedicated function directly after user creation in the view keeps logic clear and easier to debug.
  3. Final Answer:

    Call a dedicated function directly after user creation in the view -> Option C
  4. Quick Check:

    Complex logic = direct calls, not signals [OK]
Hint: Use direct calls for complex logic, signals for simple tasks [OK]
Common Mistakes:
  • Putting complex logic inside signals
  • Overriding save for unrelated tasks
  • Relying on signals for all side effects