简体   繁体   English

除了models.CASCADE 用于Django 模型中的ForeignKey 之外,还有其他on_delete 选项吗?

[英]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 constraint ON DELETE CASCADE and also deletes the object containing the ForeignKey . Django 在ON DELETE CASCADE上模拟 SQL 约束的行为,并删除包含ForeignKey的 object 。 (…) (……)

  • PROTECT : Prevent deletion of the referenced object by raising ProtectedError , a subclass of django.db.IntegrityError . PROTECT :通过引发 django.db.IntegrityError 的子类ProtectedError来防止删除引用的django.db.IntegrityError

  • RESTRICT : Prevent deletion of the referenced object by raising RestrictedError (a subclass of django.db.IntegrityError ). RESTRICT :通过引发RestrictedError ( django.db.IntegrityError 的子类)来防止删除引用的django.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.PROTECT不同,如果引用的 object 还引用了在同一操作中被删除但通过 CASCADE 关系的不同 object,则允许删除引用的 object。 (…) (……)

  • SET_NULL : Set the ForeignKey null ; SET_NULL :设置ForeignKey null this is only possible if null is True .这只有在nullTrue时才有可能。

  • SET_DEFAULT : Set the ForeignKey to its default value; SET_DEFAULT :将ForeignKey设置为其默认值; a default for the ForeignKey must be set.必须设置 ForeignKey 的default

  • 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()的值,或者如果传入了可调用对象,则为调用它的结果。 (…) (……)

  • 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约束添加到数据库字段。

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", and prior, you did not have to set an on_delete=… parameter: CASCADE was used as default value.在“早期”, 和之前,您不必设置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.

相关问题 Django 模型 - 如何取消 on_delete=models.CASCADE - Django models - how to cancel on_delete=models.CASCADE Django 正确使用 Models.Cascade (on_delete) - Django Correct Usage of Models.Cascade (on_delete) on_delete=models.PROTECT 和 on_delete=models.CASCADE 对 Django 模型有什么作用? - what does on_delete=models.PROTECT and on_delete=models.CASCADE do on Django models? Django on_delete=models.CASCADE 在 SQL 级别没有影响 - Django on_delete=models.CASCADE has no effect at SQL level 'OperationalError ' 当我插入时: owner = models.ForeignKey(User,on_delete=models.CASCADE) - 'OperationalError ' when I insert : owner = models.ForeignKey(User,on_delete=models.CASCADE) Django on_delete=models.CASCADE 在使用相关名称时不起作用 - Django on_delete=models.CASCADE not working when using related_name 不使用 user = models.OneToOneField(User,on_delete=models.CASCADE) - Not using user = models.OneToOneField(User,on_delete=models.CASCADE) 为什么我的模型的“on_delete = models.CASCADE”不生成级联外键约束? - Why doesn't my model's “on_delete=models.CASCADE,” generate a cascading foreign key constraint? 我们可以只定义models.CASCADE而不使用on_delete吗? - Can we define models.CASCADE only without using on_delete? 在 admin-django 和 models.CASCADE 中使用不同的删除行为 - Use different delete behavior in admin-django and models.CASCADE
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM