About on_delete Values for Foreign Keys

A rule of thumb to set the on_delete for foreign keys and one-to-one relationships is as follows:

  • If the field can be blank and null, set on_delete=models.SET_NULL.
  • If there the field has a default value, set on_delete=models.SET_DEFAULT.
  • Otherwise, set on_delete=models.CASCADE.

Note that CASCADE deletes all children automatically if the parent is deleted.

Tips and Tricks Programming Architecture Databases Django 4.2 Django 3.2 Django 2.2 PostgreSQL MySQL