Knowledge note
When creating a Django ORM model, you’ll always need to use a foreign key mapping. Django has the following ways to handle deleting foreign key objects:
from django.db import models
Copy the code
Processing specified mode | Processing Mode Description |
---|---|
on_delete=None | When deleting data from an associated table The behavior of the Field associated with the current table |
on_delete=models.CASCADE | Delete associated data, and the association with it is also deleted |
on_delete=models.DO_NOTHING | Delete the associated data and do nothing |
on_delete=models.PROTECT | Error ProtectedError is raised when associated data is deleted |
on_delete=models.SET_NULL | Deletes associated data and sets the associated value to NULL If this field can be set to null) |
on_delete=models.SET_DEFAULT | Deletes associated data and sets the associated value to the default value The field must have a default value |
on_delete=models.SET | Delete associated data: A.SET to the specified value: models.set (value) B.SET to the return value of the executable: models.set (object) |
Life is too short to do something that no one wants. – Ash Maurya