Why is the on_delete
property of a ForeignKey in a Django model not default? That should be the case if there is no other option other than models.CASCADE
. Is there any other option for the on_delete
property?
Yes, there are multiple builtin handlers for the on_delete=…
parameter [Django-doc]. The documentation specifies:
CASCADE
Cascade deletes. Django emulates the behavior of the SQL constraintON DELETE CASCADE
and also deletes the object containing theForeignKey
. (…)
PROTECT
: Prevent deletion of the referenced object by raisingProtectedError
, a subclass ofdjango.db.IntegrityError
.
RESTRICT
: Prevent deletion of the referenced object by raisingRestrictedError
(a subclass ofdjango.db.IntegrityError
). UnlikePROTECT
, deletion of the referenced object is allowed if it also references a different object that is being deleted in the same operation, but via a CASCADE relationship. (…)
SET_NULL
: Set theForeignKey
null
; this is only possible ifnull
isTrue
.
SET_DEFAULT
: Set theForeignKey
to its default value; adefault
for the ForeignKey must be set.
SET(…)
: Set theForeignKey
to the value passed toSET()
, or if a callable is passed in, the result of calling it. (…)
DO_NOTHING
: Take no action. If your database backend enforces referential integrity, this will cause an IntegrityError unless you manually add an SQLON DELETE
constraint to the database field.
Furthermore you can also write your own handler for the on_delete=…
parameter. For example in this article, I discuss implementing a handler that is to some extent the same as a SET(…)
but the callable it uses accepts as parameter the object that should be updated.
In the "early days", django-1.8 and prior, you did not have to set an on_delete=…
parameter: CASCADE
was used as default value. But this makes it rather implicit what should happen in case the referenced object is removed, so later they made the parameter mandatory.