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 you want to avoid accidental deletions, set
on_delete=models.PROTECT. - If the field can be blank and null, set
on_delete=models.SET_NULL. - If the field has a default value, set
on_delete=models.SET(get_default_value). - 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
Also by me
Django Messaging 🎅🏼
For Django-based social platforms.
Django Paddle Subscriptions 🎅🏼
For Django-based SaaS projects.
Django GDPR Cookie Consent 🎅🏼
For Django websites that use cookies.