0
0
Djangoframework~10 mins

pre_delete and post_delete 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 for actions before deleting an object.

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

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

Django
post_delete.connect([1], sender=Book)
Drag options to blanks, or click blank then click option'
Ahandle_save
Bhandle_delete
Chandle_pre_delete
Dhandle_post_delete
Attempts:
3 left
💡 Hint
Common Mistakes
Using a function name for pre_delete instead of post_delete
Using unrelated function names
3fill in blank
hard

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

Django
def my_handler([1], instance, **kwargs):
    print(f"Deleting {instance}")
Drag options to blanks, or click blank then click option'
Asender
Bself
Crequest
Dobj
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'self' as first argument in signal handlers
Using instance as first argument
4fill in blank
hard

Fill both blanks to create a pre_delete signal handler that prints the instance being deleted and connect it to the Author model.

Django
def [1](sender, instance, **kwargs):
    print(f"About to delete [2]")

pre_delete.connect(delete_notice, sender=Author)
Drag options to blanks, or click blank then click option'
Adelete_notice
Binstance
Csender
DAuthor
Attempts:
3 left
💡 Hint
Common Mistakes
Mismatching function name and connect argument
Printing sender instead of instance
5fill in blank
hard

Fill all three blanks to create a post_delete handler that logs the deleted object's ID and connect it to the Publisher model.

Django
def [1](sender, instance, [2]):
    print(f"Deleted object ID: {instance.[3]")

post_delete.connect(log_deletion, sender=Publisher)
Drag options to blanks, or click blank then click option'
Alog_deletion
B**kwargs
Cid
Dinstance
Attempts:
3 left
💡 Hint
Common Mistakes
Omitting **kwargs parameter
Printing wrong attribute instead of id