Recall & Review
beginner
What does the
CASCADE option do in Django's on_delete?The
CASCADE option deletes all related objects automatically when the referenced object is deleted. It's like a chain reaction removing dependent data.Click to reveal answer
beginner
Explain the
PROTECT option in Django's on_delete.PROTECT prevents deletion of the referenced object if there are related objects pointing to it. It raises an error to protect data integrity.Click to reveal answer
beginner
What happens when you use
SET_NULL in Django's on_delete?Using
SET_NULL sets the foreign key field to NULL when the referenced object is deleted. The related object stays but loses the link.Click to reveal answer
intermediate
Which
on_delete option requires the foreign key field to allow NULL values?SET_NULL requires the foreign key field to have null=True because it sets the field to NULL on deletion.Click to reveal answer
intermediate
Why would you choose
PROTECT over CASCADE in Django?You choose
PROTECT to avoid accidental deletion of important data. It forces you to handle related objects before deleting the main one.Click to reveal answer
What does
on_delete=models.CASCADE do when the referenced object is deleted?✗ Incorrect
CASCADE deletes related objects automatically to keep data consistent.Which
on_delete option stops deletion if related objects exist?✗ Incorrect
PROTECT raises an error to prevent deletion if related objects exist.When using
SET_NULL, what must be true about the foreign key field?✗ Incorrect
SET_NULL sets the field to NULL, so null=True is required.If you want to keep related objects but remove their link to a deleted object, which
on_delete do you use?✗ Incorrect
SET_NULL removes the link by setting the foreign key to NULL.Which
on_delete option can cause a chain of deletions?✗ Incorrect
CASCADE deletes related objects, which can trigger further deletions.Describe the differences between
CASCADE, PROTECT, and SET_NULL in Django's on_delete.Think about what happens to related objects when the main object is deleted.
You got /3 concepts.
When would you use
PROTECT instead of CASCADE or SET_NULL?Consider situations where deleting related data could cause problems.
You got /3 concepts.