0
0
Djangoframework~5 mins

On_delete options (CASCADE, PROTECT, SET_NULL) in Django - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
ADeletes all related objects automatically
BPrevents deletion and raises an error
CSets the foreign key to NULL
DDoes nothing to related objects
Which on_delete option stops deletion if related objects exist?
ASET_NULL
BCASCADE
CDO_NOTHING
DPROTECT
When using SET_NULL, what must be true about the foreign key field?
AIt must have <code>null=True</code>
BIt must be unique
CIt must be indexed
DIt must have <code>blank=True</code>
If you want to keep related objects but remove their link to a deleted object, which on_delete do you use?
ACASCADE
BSET_NULL
CPROTECT
DRESTRICT
Which on_delete option can cause a chain of deletions?
ASET_NULL
BPROTECT
CCASCADE
DDO_NOTHING
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.