Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'signal' instead of 'receiver' for import.
Trying to import 'connect' which is a method, not a decorator.
✗ Incorrect
The receiver decorator is imported from django.dispatch to connect functions to signals.
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'pre_save' which runs before saving.
Confusing delete signals with save signals.
✗ Incorrect
The post_save signal triggers after a model instance is saved, so the function listens to it.
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a string like 'auth.User' instead of the User class.
Using lowercase 'user' which is undefined.
✗ Incorrect
The sender must be the User model class, usually imported as 'User', not a string or module path.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using post_save signal which is unrelated to deletion.
Using User model instead of Article.
✗ Incorrect
Use pre_delete signal and the Article model class as sender to listen before an article is deleted.
5fill in blank
hardFill 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'
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.
✗ Incorrect
The m2m_changed signal listens to many-to-many changes; sender is Post.tags.through; action 'post_add' means tags were added.