Django模型ForeignKey on_delete属性:是否真的删除所有相关对象? [英] Django models ForeignKey on_delete attribute: does it really delete all related objects?

查看:497
本文介绍了Django模型ForeignKey on_delete属性:是否真的删除所有相关对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里有一些模型:

class UserProfile(models.Model):  
    name = models.CharField(max_length=30)
    email = models.EmailField(unique=True, db_index=True) 
    birthday = models.DateField()

class Photo(models.Model):
    user = models.ForeignKey(UserProfile)
    description = models.TextField(blank=True)
    photo = models.ImageField(upload_to='img/photo')



当用户删除自己时,所有这些10 照片数据库行会自动删除? (我已阅读文档,但英文不是我的母语,所以我不明白有关 on_delete 属性。)

Let's say a user has 10 photos (10 objects of Photo model). When user deletes himself will all those 10 Photo database rows delete themselves automatically? (I've read docs, but English is not my native language, so I didn't understand everything about the on_delete attribute.)

推荐答案

这是默认行为,是的。您可以通过 on_delete

that's the default behaviour, yes. you can change this behaviour with on_delete

以获取插图,尝试删除管理员中的用户配置文件。它首先显示一个警告页面,列出所有相关对象也将被删除

to get an illustration of this, try deleting a userprofile in the admin. it first shows a warning page, listing all related objects that will also get deleted

这篇关于Django模型ForeignKey on_delete属性:是否真的删除所有相关对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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