关于python:关于Django模型的on_delete = models.PROTECT和on_delete = models.CASCADE是做什么的?

what does on_delete=models.PROTECT and on_delete=models.CASCADE do on Django models?

我对Django很熟悉,但最近注意到有一个关于模型的on-delete=models.cascade和on-delete=models.protect选项,

  • 在"delete=models.cascade"和"delete=models.protect"上,两者都在做相同的事情。
  • 或者两者都是相同的(我只在删除=models.cascade时使用,当我删除父条目时,它将删除所有相关条目)

    我已经搜索了相同的文档,但找不到超过,

Django 2.0

A many-to-one relationship. Requires two positional arguments: the
class to which the model is related and the on_delete option.
To create a recursive relationship – an object that has a many-to-one relationship with itself – use models.ForeignKey('self',
on_delete=models.CASCADE).


  • CASCADEdeletes级联。Django的emulates行为杂志SQL constraint ON DELETE CASCADE也deletes对象在ForeignKey含。

  • PROTECT引用的对象被删除,防止raisingof theProtectedErrordjango.db.IntegrityError亚纲。

因为一旦你删除的东西让你改变你的模型需要的makemigrationsmigrate看到的变化。