0
0
Djangoframework~20 mins

When signals are appropriate vs not in Django - Practice Questions

Choose your learning style9 modes available
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.