0
0
Djangoframework~10 mins

On_delete options (CASCADE, PROTECT, SET_NULL) in Django - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - On_delete options (CASCADE, PROTECT, SET_NULL)
Delete Parent Object
Check on_delete option
CASCADE
End
When a parent object is deleted, Django checks the on_delete option to decide what happens to related child objects.
Execution Sample
Django
class Book(models.Model):
    author = models.ForeignKey(Author, on_delete=models.CASCADE)

# Deleting an Author triggers on_delete behavior on Book
This code shows a Book linked to an Author. Deleting the Author triggers the on_delete rule on Book.
Execution Table
StepActionon_delete OptionEffect on Child ObjectResult
1Delete Author objectCASCADEDelete all related Book objectsBooks deleted automatically
2Delete Author objectPROTECTPrevent deletion if related Book existsError raised, Author not deleted
3Delete Author objectSET_NULLSet Book.author to NULL if allowedAuthor deleted, Books remain with author=NULL
4Delete Author objectOther (e.g. DO_NOTHING)No automatic action on BookPossible DB error if FK constraint violated
5End--Process complete
💡 Process ends after applying the on_delete rule to child objects.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
Author ExistsTrueFalse (CASCADE)True (PROTECT)False (SET_NULL)Depends on option
Book Count50 (CASCADE)5 (PROTECT)5 (SET_NULL, author=NULL)Depends on option
Book.author FKAuthor IDN/A (deleted)Author IDNULLDepends on option
Key Moments - 3 Insights
Why does PROTECT prevent deleting the parent object?
PROTECT raises an error if child objects exist, stopping deletion. See execution_table row 2 where deletion is blocked.
What happens to child objects with SET_NULL if the FK does not allow null?
If FK disallows null, SET_NULL causes a database error. The child FK must allow null for SET_NULL to work, as shown in execution_table row 3.
Does CASCADE delete only direct children or deeper related objects too?
CASCADE deletes direct children and can cascade further if those children have on_delete CASCADE. The example shows direct children deleted at step 1.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what happens to Book objects when Author is deleted with CASCADE?
ABooks are deleted automatically
BBooks remain unchanged
CBooks' author field is set to NULL
DDeletion is blocked with an error
💡 Hint
See execution_table row 1 under 'Effect on Child Object'
At which step does deletion fail due to PROTECT?
AStep 3
BStep 1
CStep 2
DStep 4
💡 Hint
Check execution_table row 2 where error is raised
If Book.author FK does not allow null, what happens with SET_NULL on delete?
ABooks are deleted
BAuthor deletion is blocked
CBooks' author field is set to NULL
DNo effect, deletion proceeds
💡 Hint
See key_moments question 2 about FK nullability and SET_NULL
Concept Snapshot
Django on_delete options control child object behavior when parent is deleted:
- CASCADE: deletes child objects automatically
- PROTECT: blocks deletion if children exist
- SET_NULL: sets FK to NULL if allowed
Choose based on desired data integrity and app logic.
Full Transcript
In Django, when you delete a parent object linked by a ForeignKey, the on_delete option decides what happens to related child objects. CASCADE deletes all related children automatically. PROTECT stops deletion and raises an error if children exist. SET_NULL sets the child's foreign key to NULL if the field allows nulls. Other options exist but these three are common. This visual shows step-by-step what happens when deleting a parent with each option, how variables like object existence and foreign keys change, and common beginner questions about behavior and errors. Understanding these helps keep your database consistent and avoid unexpected deletions or errors.