[英]Is there any other option for on_delete other than models.CASCADE for a ForeignKey in Django models?
Why is the on_delete
property of a ForeignKey in a Django model not default?为什么 Django model 中 ForeignKey 的
on_delete
属性不是默认值? That should be the case if there is no other option other than models.CASCADE
.如果除
models.CASCADE
之外没有其他选项,则应该是这种情况。 Is there any other option for the on_delete
property? on_delete
属性还有其他选项吗?
Yes, there are multiple builtin handlers for the on_delete=…
parameter [Django-doc] .是的,
on_delete=…
参数 [Django-doc]有多个内置处理程序。 The documentation specifies:该文档指定:
CASCADE
Cascade deletes.CASCADE
级联删除。 Django emulates the behavior of the SQL constraintON DELETE CASCADE
and also deletes the object containing theForeignKey
.Django 在
ON DELETE CASCADE
上模拟 SQL 约束的行为,并删除包含ForeignKey
的 object 。 (…)(……)
PROTECT
: Prevent deletion of the referenced object by raisingProtectedError
, a subclass ofdjango.db.IntegrityError
.PROTECT
:通过引发 django.db.IntegrityError 的子类ProtectedError
来防止删除引用的django.db.IntegrityError
。
RESTRICT
: Prevent deletion of the referenced object by raisingRestrictedError
(a subclass ofdjango.db.IntegrityError
).RESTRICT
:通过引发RestrictedError
( django.db.IntegrityError 的子类)来防止删除引用的django.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.与
PROTECT
不同,如果引用的 object 还引用了在同一操作中被删除但通过 CASCADE 关系的不同 object,则允许删除引用的 object。 (…)(……)
SET_NULL
: Set theForeignKey
null
;SET_NULL
:设置ForeignKey
null
; this is only possible ifnull
isTrue
.这只有在
null
为True
时才有可能。
SET_DEFAULT
: Set theForeignKey
to its default value;SET_DEFAULT
:将ForeignKey
设置为其默认值; adefault
for the ForeignKey must be set.必须设置 ForeignKey 的
default
。
SET(…)
: Set theForeignKey
to the value passed toSET()
, or if a callable is passed in, the result of calling it.SET(…)
:将ForeignKey
设置为传递给SET()
的值,或者如果传入了可调用对象,则为调用它的结果。 (…)(……)
DO_NOTHING
: Take no action.DO_NOTHING
:不采取任何行动。 If your database backend enforces referential integrity, this will cause an IntegrityError unless you manually add an SQLON DELETE
constraint to the database field.如果您的数据库后端强制执行引用完整性,这将导致 IntegrityError,除非您手动将 SQL
ON DELETE
约束添加到数据库字段。
Furthermore you can also write your own handler for the on_delete=…
parameter.此外,您还可以为
on_delete=…
参数编写自己的处理程序。 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.例如,在本文中,我讨论了实现一个在某种程度上与
SET(…)
相同的处理程序,但它使用的可调用对象接受应更新的 object 作为参数。
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.在“早期”, django-1.8和之前,您不必设置
on_delete=…
参数: CASCADE
被用作默认值。 But this makes it rather implicit what should happen in case the referenced object is removed, so later they made the parameter mandatory.但这使得在引用的 object 被删除的情况下应该发生的事情变得相当隐含,所以后来他们使参数成为强制性参数。
Pulled these from djangos documentation here: https://docs.djangoproject.com/en/4.0/ref/models/fields/#foreignkey从这里的 djangos 文档中提取这些: https://docs.djangoproject.com/en/4.0/ref/models/fields/#foreignkey
They have code examples too.他们也有代码示例。
The possible values for on_delete are found in django.db.models: on_delete 的可能值在 django.db.models 中找到:
CASCADE Cascade deletes. CASCADE 级联删除。 Django emulates the behavior of the SQL constraint ON DELETE CASCADE and also deletes the object containing the ForeignKey.
Django 在 DELETE CASCADE 上模拟 SQL 约束的行为,并删除包含 ForeignKey 的 object。
Model.delete() isn't called on related models, but the pre_delete and post_delete signals are sent for all deleted objects. Model.delete() 不会在相关模型上调用,但会为所有已删除的对象发送 pre_delete 和 post_delete 信号。
PROTECT Prevent deletion of the referenced object by raising ProtectedError, a subclass of django.db.IntegrityError. PROTECT 通过引发 django.db.IntegrityError 的子类 ProtectedError 来防止删除引用的 object。
RESTRICT Prevent deletion of the referenced object by raising RestrictedError (a subclass of django.db.IntegrityError). RESTRICT 通过引发 RestrictedError(django.db.IntegrityError 的子类)来防止删除引用的 object。 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.
与 PROTECT 不同,如果引用的 object 还引用了在同一操作中被删除的不同 object,则允许删除引用的 object,但通过 CASCADE 关系。
SET_DEFAULT Set the ForeignKey to its default value; SET_DEFAULT 将 ForeignKey 设置为其默认值; a default for the ForeignKey must be set.
必须设置 ForeignKey 的默认值。
SET() Set the ForeignKey to the value passed to SET(), or if a callable is passed in, the result of calling it. SET() 将 ForeignKey 设置为传递给 SET() 的值,或者如果传入了可调用对象,则为调用它的结果。 In most cases, passing a callable will be necessary to avoid executing queries at the time your models.py is imported:
在大多数情况下,需要传递一个可调用对象以避免在导入 models.py 时执行查询:
DO_NOTHING Take no action. DO_NOTHING 不采取任何行动。 If your database backend enforces referential integrity, this will cause an IntegrityError unless you manually add an SQL ON DELETE constraint to the database field.
如果您的数据库后端强制执行引用完整性,这将导致 IntegrityError,除非您手动将 SQL ON DELETE 约束添加到数据库字段。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.