级联更新和删除wih django [英] Cascade on update and delete wih django

查看:761
本文介绍了级联更新和删除wih django的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在模型api中找不到级联更新或删除的选项。我想知道是否有一个手册,可以帮助我通过这个数据库选项在sycning数据库。

I cannot find an option to cascade update or delete in the model api. Am wondering if there is a manual out there that can help me pass this db option when sycning the db.

任何想法?

推荐答案

查看 ForeignKey docs。它说:

Look at the ForeignKey docs. It says:


当一个ForeignKey引用的对象被删除时,Django默认模拟SQL约束在DELETE CASCADE上的行为,删除包含ForeignKey的对象。可以通过指定on_delete参数来覆盖此行为。

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.

所以默认行为是级联在删除上,但您可以通过指定如下:

So the default behavior is to cascade on delete, but you can change it by specifying something like this:

class Foobar(models.Model):
    user = models.ForeignKey(User, on_delete=models.SET(User.objects.get_or_create(username="foooobarrrr")[0]))

这篇关于级联更新和删除wih django的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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