Django模型ForeignKey on_delete属性:完整含义? [英] Django models ForeignKey on_delete attribute: full meaning?

查看:81
本文介绍了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张照片(10个 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天全站免登陆