Bird
Raised Fist0
Djangoframework~5 mins

pre_delete and post_delete signals in Django - Cheat Sheet & Quick Revision

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What is the purpose of the pre_delete signal in Django?
The pre_delete signal is sent just before a model instance is deleted. It allows you to run custom code or cleanup tasks before the actual deletion happens.
Click to reveal answer
beginner
When is the post_delete signal triggered in Django?
The post_delete signal is triggered immediately after a model instance has been deleted from the database. It is useful for actions that should happen after deletion, like clearing caches or logging.
Click to reveal answer
intermediate
How do you connect a function to the pre_delete signal for a model?
You use the @receiver(pre_delete, sender=YourModel) decorator above your function or call pre_delete.connect(your_function, sender=YourModel). This tells Django to run your function before deleting instances of that model.
Click to reveal answer
intermediate
Can you stop the deletion of an object inside a pre_delete signal handler?
No, the pre_delete signal does not support stopping the deletion. It is only for running code before deletion. To prevent deletion, you should override the model's delete() method or use database constraints.
Click to reveal answer
beginner
Give an example use case for the post_delete signal.
A common use case is deleting related files from the file system after a model instance with a file field is deleted. The post_delete signal lets you remove those files safely after the database record is gone.
Click to reveal answer
Which signal runs before a Django model instance is deleted?
Apost_delete
Bpre_delete
Cpre_save
Dpost_save
What can you do inside a post_delete signal handler?
ARun code after the instance is deleted
BModify the instance before deletion
CPrevent the deletion
DCancel the deletion process
How do you connect a function to a Django signal?
ABy calling the signal directly in templates
BBy overriding the model's save() method
CUsing @receiver decorator or connect() method
DBy importing the signal in views.py
Can pre_delete signal stop the deletion of an object?
ANo, it only runs before deletion
BYes, by raising an exception
CYes, by returning False
DYes, by calling cancel()
Which of these is a good use for post_delete?
APreventing deletion of an object
BValidating data before saving
CModifying data before saving
DDeleting related files after record deletion
Explain the difference between pre_delete and post_delete signals in Django.
Think about when each signal is triggered relative to the deletion event.
You got /4 concepts.
    Describe how to connect a function to the pre_delete signal for a Django model and what you might use it for.
    Focus on the connection method and practical uses.
    You got /3 concepts.

      Practice

      (1/5)
      1. What is the main difference between Django's pre_delete and post_delete signals?
      easy
      A. pre_delete runs after a record is deleted, post_delete runs before.
      B. pre_delete runs before a record is deleted, post_delete runs after.
      C. Both signals run at the same time during deletion.
      D. pre_delete only works with models, post_delete only with forms.

      Solution

      1. Step 1: Understand signal timing

        pre_delete is triggered just before a model instance is deleted from the database.
      2. Step 2: Understand post_delete timing

        post_delete is triggered immediately after the instance has been deleted.
      3. Final Answer:

        pre_delete runs before a record is deleted, post_delete runs after. -> Option B
      4. Quick Check:

        Signal timing difference = pre_delete runs before a record is deleted, post_delete runs after. [OK]
      Hint: Remember: pre_delete before removal, post_delete after removal [OK]
      Common Mistakes:
      • Confusing the order of signal execution
      • Thinking both signals run simultaneously
      • Assuming signals work only with forms
      2. Which of the following is the correct way to connect a pre_delete signal to a model named Book?
      easy
      A. Book.pre_delete.connect(my_handler)
      B. post_delete.connect(my_handler, sender=Book)
      C. pre_delete.connect(my_handler, sender=Book)
      D. connect(pre_delete, my_handler, Book)

      Solution

      1. Step 1: Recall signal connection syntax

        In Django, signals are connected using signal.connect(handler, sender=Model).
      2. Step 2: Apply to pre_delete and Book

        Use pre_delete.connect(my_handler, sender=Book) to connect the handler to the Book model.
      3. Final Answer:

        pre_delete.connect(my_handler, sender=Book) -> Option C
      4. Quick Check:

        Correct connect syntax = pre_delete.connect(my_handler, sender=Book) [OK]
      Hint: Use signal.connect(handler, sender=Model) to connect signals [OK]
      Common Mistakes:
      • Using post_delete instead of pre_delete
      • Trying to call connect on the model itself
      • Incorrect argument order in connect
      3. Given this code snippet, what will be printed when a Book instance is deleted?
      from django.db.models.signals import pre_delete, post_delete
      from django.dispatch import receiver
      
      @receiver(pre_delete, sender=Book)
      def before_delete(sender, instance, **kwargs):
          print(f"Deleting book: {instance.title} (pre_delete)")
      
      @receiver(post_delete, sender=Book)
      def after_delete(sender, instance, **kwargs):
          print(f"Deleted book: {instance.title} (post_delete)")
      
      book = Book(title='Django Basics')
      book.delete()
      medium
      A. Deleting book: Django Basics (pre_delete) Deleted book: Django Basics (post_delete)
      B. Deleted book: Django Basics (post_delete) Deleting book: Django Basics (pre_delete)
      C. Only Deleting book: Django Basics (pre_delete) is printed
      D. No output is printed

      Solution

      1. Step 1: Understand signal order on deletion

        pre_delete runs before the instance is deleted, so its print runs first.
      2. Step 2: Understand post_delete runs after deletion

        After deletion, post_delete signal triggers, printing the second message.
      3. Final Answer:

        Deleting book: Django Basics (pre_delete) Deleted book: Django Basics (post_delete) -> Option A
      4. Quick Check:

        Signal print order = Deleting book: Django Basics (pre_delete) Deleted book: Django Basics (post_delete) [OK]
      Hint: pre_delete prints before delete, post_delete prints after [OK]
      Common Mistakes:
      • Assuming post_delete prints before pre_delete
      • Expecting only one signal to run
      • Forgetting to call delete() on the instance
      4. What is wrong with this code that tries to use pre_delete signal?
      from django.db.models.signals import pre_delete
      
      @pre_delete(sender=Author)
      def cleanup(sender, instance, **kwargs):
          print('Cleaning up author data')
      medium
      A. Using @pre_delete decorator is incorrect; should use @receiver instead.
      B. Missing import for Author model.
      C. Signal handler must return a value.
      D. The function name 'cleanup' is reserved and cannot be used.

      Solution

      1. Step 1: Check signal handler decoration

        Django signals use the @receiver(signal, sender=Model) decorator, not @pre_delete(sender=Model).
      2. Step 2: Confirm correct decorator usage

        Replace @pre_delete(sender=Author) with @receiver(pre_delete, sender=Author) to fix the error.
      3. Final Answer:

        Using @pre_delete decorator is incorrect; should use @receiver instead. -> Option A
      4. Quick Check:

        Use @receiver for signals, not @pre_delete [OK]
      Hint: Use @receiver(signal, sender=Model) to decorate signal handlers [OK]
      Common Mistakes:
      • Using signal name as decorator directly
      • Forgetting to import @receiver
      • Assuming signal handlers must return values
      5. You want to automatically delete all Comment objects related to a Post before the Post itself is deleted. Which signal and approach is best?
      hard
      A. Use post_delete on Comment to delete the post after comments are removed.
      B. Use post_delete on Post to delete related Comment objects after the post is removed.
      C. Use pre_delete on Comment to delete the post before comments are removed.
      D. Use pre_delete on Post to delete related Comment objects before the post is removed.

      Solution

      1. Step 1: Understand deletion order requirement

        Comments must be deleted before the post to avoid foreign key errors.
      2. Step 2: Choose correct signal and model

        pre_delete on Post allows deleting related comments before the post is removed.
      3. Final Answer:

        Use pre_delete on Post to delete related Comment objects before the post is removed. -> Option D
      4. Quick Check:

        Delete related objects in pre_delete to avoid FK errors [OK]
      Hint: Delete related objects in pre_delete to prevent FK constraint errors [OK]
      Common Mistakes:
      • Deleting related objects after post deletion causes errors
      • Using signals on wrong model
      • Trying to delete parent in comment signals