on_delete in Django models

Below are the possible actions with on_delete in Django Models:

  • CASCADE: When the referenced object is deleted, also delete the objects that have references to it.
    Ex: When you remove a Sales Order Header for instance, it will delete Sales Order Lines as well). 
  • PROTECT: To delete it you will have to delete all objects that reference it manually.
  • RESTRICT: Similar behavior as PROTECT that matches more accurately. 
  • SET_NULL: Set the reference to NULL. For instance, when you delete a User, you might want to keep the comments he posted on blog posts, but say it was posted by an anonymous or deleted user. 
  • SET_DEFAULT: Set the default value.
  • SET: Set a given value.
  • DO_NOTHING: This will create integrity issues in your database.

Comments

Popular posts from this blog

Internationalization (Language Setting) in Django

reverse_lazy() Method in Django

CharFilter in Django