Bird
0
0

You want to delete related Photo objects before deleting a Gallery instance. Which signal and approach is best?

hard📝 lifecycle Q8 of 15
Django - Signals
You want to delete related Photo objects before deleting a Gallery instance. Which signal and approach is best?
AUse <code>post_delete</code> on <code>Gallery</code> to delete related <code>Photo</code> objects
BUse <code>pre_delete</code> on <code>Gallery</code> to delete related <code>Photo</code> objects
CUse <code>pre_save</code> on <code>Photo</code> to delete <code>Gallery</code>
DUse <code>post_save</code> on <code>Gallery</code> to delete related <code>Photo</code> objects
Step-by-Step Solution
Solution:
  1. Step 1: Determine when to delete related objects

    Related Photo objects must be deleted before the Gallery instance is removed to avoid orphaned data.
  2. Step 2: Choose the correct signal

    pre_delete runs before the Gallery is deleted, allowing safe deletion of related Photo objects.
  3. Final Answer:

    Use pre_delete on Gallery to delete related Photo objects -> Option B
  4. Quick Check:

    Delete related data in pre_delete [OK]
Quick Trick: Delete related objects in pre_delete to avoid errors [OK]
Common Mistakes:
MISTAKES
  • Deleting related objects after main deletion
  • Using save signals instead of delete signals
  • Deleting parent in child's signal

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Django Quizzes