假设我有一本模型书:
class Books(models.Model):
name = models.Charfield()
author = models.Charfield()
后来我建立了另一个叫做BookReviews的模型,它链接到书籍:
class BookReviews(models.Model):
book = models.ForeignKey(Books)
content = models.TextField()
但我搞砸了,我想完全删除书评范本。当我跑的时候
python manage.py migrate
我收到一条警告信息:
Any objects related to these content types by a foreign key will also be deleted. Are you sure you want to delete these content types?
这是否意味着,即使那些条目在书评之前就已经存在,任何链接到书中的条目也会被删除?你知道吗
我想你想知道
on_delete
中的ForeignKey
选项删除
Books
对象时,会删除所有外部BookReviews
对象,因为on_delete=models.CASCADE
是默认值。您的型号如下如果不想自动删除
BookReviews
,可以使用其他on_delete
选项,如下所示django文档中有许多有用的示例,因此请查看here以获取更多信息。你知道吗
相关问题 更多 >
编程相关推荐