简体   繁体   中英

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM