我在模型 api 中找不到级联更新或删除的选项。我想知道是否有手册可以帮助我在同步数据库时传递此数据库选项。
有什么想法吗?
最佳答案
查看ForeignKey文档。它说:
When an object referenced by a ForeignKey is deleted, Django by default emulates the behavior of the SQL constraint ON DELETE CASCADE and also deletes the object containing the ForeignKey. This behavior can be overridden by specifying the on_delete argument.
所以默认行为是级联删除,但您可以通过指定如下内容来更改它:
class Foobar(models.Model):
user = models.ForeignKey(User, on_delete=models.SET(User.objects.get_or_create(username="foooobarrrr")[0]))
关于python - 使用 Django 级联更新和删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7295417/