我想从表Video中删除元素,并向我返回此错误,
Deleting the Video 'add test video' would require deleting the following protected related objects:
Video Title: dadsadas
这是我目前的模型:
class Videos(models.Model):
Title = models.CharField(max_length=100, unique=False, help_text='video title')
class TitleVideo(models.Model):
title = models.CharField(max_length=100, unique=False,
help_text='youtube video title')
video = models.ForeignKey(Videos, related_name='video_id',
null=False, blank=False, on_delete=models.PROTECT)
我要这样做:当我从“视频”表中删除项目时,自动删除与视频项目相关的所有标题,我该怎么做?
答案 0 :(得分:1)
在删除ForeignKey
上标记CASCADE
关系:
video = models.ForeignKey(Videos, related_name='video_id',
null=False, blank=False, on_delete=models.CASCADE)
请参阅文档on_delete,以了解如果删除了父对象(此处为ForeignKey
),应该在TitleVideo
子对象(此处为Video
)上进行的操作。