我很熟悉Django,但我最近注意到存在一个on_delete=models。级联选项与模型。我已经搜索了相同的文档,但我找不到更多的东西:

在Django 1.9中更改: On_delete现在可以用作第二个位置参数(以前它通常只作为关键字参数传递)。它将是Django 2.0中必须的参数。

用法示例如下:

from django.db import models

class Car(models.Model):
    manufacturer = models.ForeignKey(
        'Manufacturer',
        on_delete=models.CASCADE,
    )
    # ...

class Manufacturer(models.Model):
    # ...
    pass

on_delete做什么?(我猜如果模型被删除了要做的动作。)

什么是模型。级联做什么?(文档中的提示)

还有什么其他的选择(如果我的猜测是正确的)?

这方面的文档放在哪里?


当前回答

删除父对象时删除数据库中的所有子字段,然后使用on_delete,如下所示:

class user(models.Model):
    commodities = models.ForeignKey(commodity, on_delete=models.CASCADE)

其他回答

如前所述,CASCADE将删除具有外键的记录,并引用已删除的另一个对象。例如,如果你有一个房地产网站,有一个引用城市的属性

class City(models.Model):
    # define model fields for a city

class Property(models.Model):
    city = models.ForeignKey(City, on_delete = models.CASCADE)
    # define model fields for a property

现在当城市从数据库中删除时,所有相关的属性(例如。位于该城市的房地产)也将从数据库中删除

现在我还想提一下其他选项的优点,比如SET_NULL或SET_DEFAULT甚至DO_NOTHING。基本上,从管理的角度来看,您希望“删除”这些记录。但你不希望它们消失。原因有很多。有人可能不小心删除了它,或者是为了审计和监控。以及简单的报道。因此,这可能是一种将房产与城市“断开”的方法。同样,这取决于您的应用程序是如何编写的。

For example, some applications have a field "deleted" which is 0 or 1. And all their searches and list views etc, anything that can appear in reports or anywhere the user can access it from the front end, exclude anything that is deleted == 1. However, if you create a custom report or a custom query to pull down a list of records that were deleted and even more so to see when it was last modified (another field) and by whom (i.e. who deleted it and when)..that is very advantageous from the executive standpoint.

不要忘记,对于这些记录,您可以恢复意外删除,只需删除= 0即可。

我的观点是,如果有一个功能,它背后总是有一个原因。这并不总是一个好理由。但是有一个原因。而且通常都是很好的。

on_delete方法用于告诉Django如何处理依赖于你删除的模型实例的模型实例。(例如一个ForeignKey关系)。on_delete =模型。CASCADE告诉Django级联删除效果,即继续删除依赖模型。

Here's a more concrete example. Assume you have an Author model that is a ForeignKey in a Book model. Now, if you delete an instance of the Author model, Django would not know what to do with instances of the Book model that depend on that instance of Author model. The on_delete method tells Django what to do in that case. Setting on_delete=models.CASCADE will instruct Django to cascade the deleting effect i.e. delete all the Book model instances that depend on the Author model instance you deleted.

注意:on_delete将在Django 2.0中成为必选参数。在旧版本中,它默认为CASCADE。

这是完整的官方文件。

删除父对象时删除数据库中的所有子字段,然后使用on_delete,如下所示:

class user(models.Model):
    commodities = models.ForeignKey(commodity, on_delete=models.CASCADE)

重新定位你对“CASCADE”功能的思维模式,将FK添加到一个已经存在的CASCADE(即瀑布)中。这个瀑布的源是一个主键(PK)。删除向下流动。

因此,如果您将一个FK的on_delete定义为“CASCADE”,那么您将把这个FK的记录添加到一个源于PK的级联删除中。FK的记录可能参与这个级联,也可能不参与(“SET_NULL”)。事实上,具有FK的记录甚至可能阻止删除流!用“PROTECT”建造一座大坝。

这是你的问题的答案,说:为什么我们使用on_delete?

当一个被ForeignKey引用的对象被删除时,Django默认会模仿SQL约束ON DELETE CASCADE的行为,同时也会删除包含ForeignKey的对象。可以通过指定on_delete参数来重写此行为。例如,如果你有一个可为空的ForeignKey,并且你想在删除引用对象时将它设置为空:

user = models.ForeignKey(User, blank=True, null=True, on_delete=models.SET_NULL)

on_delete的可能值可以在django.db.models中找到:

CASCADE:级联删除;默认值。

PROTECT:通过引发ProtectedError (django.db.IntegrityError的子类)来防止被引用对象的删除。

SET_NULL:设置ForeignKey为空;这只有在null为True时才有可能。

SET_DEFAULT:设置ForeignKey为默认值;必须设置一个默认的ForeignKey。