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

标签 python django

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

相关文章:

python - 访问 token 刷新错误: invalid_grant from oauth2client

python - 如何在 Pandas df/series 上使用 .groupby() 后预览行

python - 消除列表中的重复元素

python - 修改函数功能的最Pythonic方式是什么?

Django:JSONField + 全文搜索 + 索引 -> 序列扫描。如何配置索引工作?

python - celery 运行作为守护进程停止

python - 用于获取 RSS 的 Django cron

python - Django 不发送错误邮件——我该如何调试?

尝试使用 devserver 提供静态文件时,Django channel 出错

Django ListView : How can I create a conditional attribute that's accessible in my template?