在Django机型上on_delete做什么? [英] what does on_delete do on Django models?

查看:178
本文介绍了在Django机型上on_delete做什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很熟悉Django,但最近注意到,模型中存在一个 on_delete = models.CASCADE 选项,我已经搜索了相同的文档,找不到任何东西,


在Django 1.9中更改:



on_delete 现在可以用作第二个位置参数(以前通常只作为关键字参数传递)。这将是Django 2.0中必需的参数。


使用情况的例子是

 $ $ 

$ b class Car(models.Model):
制造商= models.ForeignKey(
'制造商',
on_delete = models.CASCADE,

#...

class制造商(models.Model):
#...
pass

on_delete有什么作用? (如果模型被删除,请猜测要执行的操作)



models.CASCADE做什么? (文档中的任何提示



还有其他选项可用(如果我的猜测是正确的

该文档位于哪里?

解决方案

这是行为在引用对象被删除时采用。它不是django特有的,这是一个SQL标准。



发生这种事件时,需要执行6个可能的操作:




  • CASCADE :当引用的对象被删除时,还会删除引用对象的对象(当您删除博客帖子例如,您可能还想删除评论)。 SQL等价物: CASCADE

  • PROTECT :禁止删除引用目的。要删除它,您将必须删除手动引用它的所有对象。 SQL等价物: RESTRICT

  • SET_NULL :将引用设置为NULL要求该字段为空)。例如,当您删除用户时,您可能希望保留他在博客帖子上发布的评论,但是说它是由匿名(或删除)用户发布的。 SQL等价: SET NULL

  • SET_DEFAULT :设置默认值。 SQL等价物: SET DEFAULT

  • SET(...)设定给定值。这个不是SQL标准的一部分,完全由Django处理。

  • DO_NOTHING 可能是一个非常糟糕的主意,因为这将是在数据库中引用完整性问题(引用实际上不存在的对象)。 SQL等价物: NO ACTION



资料来源: Django文档



另请参见 PostGreSQL 的文档。



在大多数情况下, CASCADE 是预期的行为,但对于每个ForeignKey,您应该总是问自己在这种情况下的预期行为是什么。 PROTECT SET_NULL 通常很有用。设置 CASCADE 它不应该,可以通过简单地删除单个用户来级联地删除所有数据库。


I'm quite familiar with Django, but recently noticed there exists a on_delete=models.CASCADE option with the models, I have searched for the documentation for the same but couldn't find anything more than,

Changed in Django 1.9:

on_delete can now be used as the second positional argument (previously it was typically only passed as a keyword argument). It will be a required argument in Django 2.0.

an example case of usage is

from django.db import models

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

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

What does on_delete does? (guess the actions to be done if the model is deleted)

What does models.CASCADE do? (any hints in documentation)

What other options are available (if my guess is correct)?

Where does the documentation for this resides?

解决方案

This is the behaviour to adopt when the referenced object is deleted. It is not specific to django, this is an SQL standard.

There are 6 possible actions to take when such event occurs:

  • CASCADE: When the referenced object is deleted, also delete the objects that have references to it (When you remove a blog post for instance, you might want to delete comments as well). SQL equivalent: CASCADE.
  • PROTECT: Forbid the deletion of the referenced object. To delete it you will have to delete all objects that reference it manually. SQL equivalent: RESTRICT.
  • SET_NULL: Set the reference to NULL (requires the field to be nullable). For instance, when you delete a User, you might want to keep the comments he posted on blog posts, but say it was posted by an anonymous (or deleted) user. SQL equivalent: SET NULL.
  • SET_DEFAULT: Set the default value. SQL equivalent: SET DEFAULT.
  • SET(...): Set a given value. This one is not part of the SQL standard and is entirely handled by Django.
  • DO_NOTHING: Probably a very bad idea since this would create integrity issues in your database (referencing an object that actually doesn't exist). SQL equivalent: NO ACTION.

Source: Django documentation

See also the documentation of PostGreSQL for instance.

In most cases, CASCADE is the expected behaviour, but for every ForeignKey, you should always ask yourself what is the expected behaviour in this situation. PROTECT and SET_NULL are often useful. Setting CASCADE where it should not, can potentially delete all your database in cascade, by simply deleting a single user.

这篇关于在Django机型上on_delete做什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆