0
0
Djangoframework~10 mins

Receiver decorator in Django - Interactive Code Practice

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