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?
>Solution :
Yes, there are multiple builtin handlers for the on_delete=… parameter [Django-doc]. The documentation specifies:
CASCADECascade deletes. Django emulates the behavior of the SQL constraintON DELETE CASCADEand 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). Unlike
PROTECT, 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 theForeignKeynull; this is only possible ifnullisTrue.
SET_DEFAULT: Set theForeignKeyto its default value; adefaultfor the ForeignKey must be set.
SET(…): Set theForeignKeyto 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 DELETEconstraint 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.