0
0
Djangoframework~10 mins

When signals are appropriate vs not in Django - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to connect a signal to a receiver function in Django.

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

@receiver([1], sender=MyModel)
def my_handler(sender, instance, **kwargs):
    print('Signal received')
Drag options to blanks, or click blank then click option'
Apre_delete
Bpost_save
Cm2m_changed
Drequest_finished
Attempts:
3 left
💡 Hint
Common Mistakes
Using a signal that triggers before deletion instead of after saving.
Confusing model signals with request signals.
2fill in blank
medium

Complete the code to disconnect a signal receiver in Django.

Django
from django.db.models.signals import post_save

post_save.[1](my_handler, sender=MyModel)
Drag options to blanks, or click blank then click option'
Adisconnect
Bsend
Cconnect
Dregister
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'connect' instead of 'disconnect' to remove a receiver.
Trying to use a method that does not exist on the signal.
3fill in blank
hard

Fix the error in the signal receiver to avoid side effects during testing.

Django
@receiver(post_save, sender=MyModel)
def my_handler(sender, instance, created, **kwargs):
    if [1]:
        send_welcome_email(instance.email)
Drag options to blanks, or click blank then click option'
Acreated
Binstance
Csender
Dkwargs
Attempts:
3 left
💡 Hint
Common Mistakes
Not checking if the instance is newly created, causing repeated actions.
Using the instance object directly as a condition.
4fill in blank
hard

Fill both blanks to create a signal receiver that avoids recursive saves.

Django
@receiver(post_save, sender=MyModel)
def update_related(sender, instance, [1], **[2]):
    if created:
        instance.related_model.update_status()
Drag options to blanks, or click blank then click option'
Acreated
Bkwargs
Cargs
Dinstance
Attempts:
3 left
💡 Hint
Common Mistakes
Missing the 'created' parameter causing unintended repeated actions.
Not including '**kwargs' leading to errors when extra arguments are passed.
5fill in blank
hard

Fill all three blanks to write a signal receiver that updates a cache only when a model instance is deleted.

Django
@receiver([1], sender=MyModel)
def clear_cache(sender, instance, [2], **[3]):
    cache.delete(f'user_cache_{instance.pk}')
Drag options to blanks, or click blank then click option'
Apre_delete
Busing
Ckwargs
Dpost_save
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'post_save' signal instead of 'pre_delete' for deletion actions.
Omitting required parameters causing runtime errors.