gpt4 book ai didi

python - 使用 Django 级联更新和删除

转载 作者:太空宇宙 更新时间:2023-11-04 06:26:58 25 4
gpt4 key购买 nike

我在模型 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com