0
0
Djangoframework~10 mins

pre_save and post_save signals 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 Django signal used before saving a model instance.

Django
from django.db.models.signals import [1]
Drag options to blanks, or click blank then click option'
Apre_save
Bpost_save
Cm2m_changed
Drequest_finished
Attempts:
3 left
💡 Hint
Common Mistakes
Confusing pre_save with post_save.
Importing unrelated signals like m2m_changed.
2fill in blank
medium

Complete the code to connect a function to the post_save signal for a model named Book.

Django
post_save.connect([1], sender=Book)
Drag options to blanks, or click blank then click option'
Asave_book
Bbook_saved_handler
Chandle_save
Dpost_save
Attempts:
3 left
💡 Hint
Common Mistakes
Using the signal name instead of a handler function.
Passing a string instead of a function reference.
3fill in blank
hard

Fix the error in the signal handler definition to accept the correct parameters.

Django
def [1](sender, instance, **kwargs):
    print(f"Saved: {instance}")
Drag options to blanks, or click blank then click option'
Apre_save_handler
Bpost_save
Cbook_saved_handler
Dhandle_save
Attempts:
3 left
💡 Hint
Common Mistakes
Using the signal name as the function name.
Mismatching handler names between connection and definition.
4fill in blank
hard

Fill both blanks to define a pre_save signal handler that modifies a model's field before saving.

Django
def [1](sender, instance, **kwargs):
    instance.title = instance.title.[2]()
Drag options to blanks, or click blank then click option'
Apre_save_handler
Bpost_save_handler
Cupper
Dlower
Attempts:
3 left
💡 Hint
Common Mistakes
Using post_save handler name for pre_save.
Using wrong string method like lower instead of upper.
5fill in blank
hard

Fill all three blanks to connect a pre_save handler and ensure it only runs for the Author model.

Django
from django.db.models.signals import [1]

[2].connect(pre_save_handler, sender=[3])
Drag options to blanks, or click blank then click option'
Apre_save
Bpost_save
CAuthor
DBook
Attempts:
3 left
💡 Hint
Common Mistakes
Importing or connecting the wrong signal.
Connecting the handler to the wrong model.