1There are seven possible actions to take when such event occurs:
2
3CASCADE: When the referenced object is deleted, also delete the objects that have references to it
4 (when you remove a blog post for instance, you might want to delete comments as well).
5 SQL equivalent: CASCADE.
6
7PROTECT: Forbid the deletion of the referenced object.
8 To delete it you will have to delete all objects that reference it manually.
9 SQL equivalent: RESTRICT.
10
11RESTRICT: (introduced in Django 3.1) Similar behavior as PROTECT that matches SQL's RESTRICT more accurately. (See django documentation example)
12SET_NULL: Set the reference to NULL (requires the field to be nullable).
13 For instance, when you delete a User, you might want to keep the comments he posted on blog posts,
14 but say it was posted by an anonymous (or deleted) user.
15 SQL equivalent: SET NULL.
16
17SET_DEFAULT: Set the default value. SQL equivalent: SET DEFAULT.
18
19SET(...): Set a given value. This one is not part of the SQL standard and is entirely handled by Django.
20
21DO_NOTHING: Probably a very bad idea since this would create integrity issues in your database
22 (referencing an object that actually doesn't exist). SQL equivalent: NO ACTION.
1on_delete=models.CASCADE will delete anything created by the admin if
2the admin user is deleted.
1CREATE TABLE child_table
2(
3 column1 datatype [ NULL | NOT NULL ],
4 column2 datatype [ NULL | NOT NULL ],
5 ...
6
7 CONSTRAINT fk_name
8 FOREIGN KEY (child_col1, child_col2, ... child_col_n)
9 REFERENCES parent_table (parent_col1, parent_col2, ... parent_col_n)
10 ON DELETE CASCADE
11 [ ON UPDATE { NO ACTION | CASCADE | SET NULL | SET DEFAULT } ]
12);