开发者

Cascade on update and delete wih django

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.

any idea'开发者_如何学编程s?


Look at the ForeignKey docs. It says:

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]))
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜