为什么我的 GenericForeignKey 删除时不会级联?
我正在创建一个自定义评论系统,它可以使用内容类型 GenericForeignKey 将评论附加到任何模型。
class Comment(models.Model):
body = models.TextField(verbose_name='Comment')
user = models.ForeignKey(User)
parent = models.ForeignKey('self', null=True, blank=True)
created = models.DateTimeField(auto_now_add=True)
content_type = models.ForeignKey(ContentType)
object_id = models.PositiveIntegerField()
content_object = generic.GenericForeignKey('content_type', 'object_id')
据我了解,当删除附加注释的模型时,删除操作应该级联并删除注释。
不幸的是,这并没有发生,我被难住了。默认删除行为发生变化是否有任何常见原因?
I'm creating a custom commenting system which can attache comments to any model using the contenttypes GenericForeignKey.
class Comment(models.Model):
body = models.TextField(verbose_name='Comment')
user = models.ForeignKey(User)
parent = models.ForeignKey('self', null=True, blank=True)
created = models.DateTimeField(auto_now_add=True)
content_type = models.ForeignKey(ContentType)
object_id = models.PositiveIntegerField()
content_object = generic.GenericForeignKey('content_type', 'object_id')
It is my understanding that when the model the comment is attached to is deleted, the delete should cascade and remove the comment as well.
Unfortunately, this isn't happening and I'm stumped. Are there any common reasons why the default delete behaviour would change?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不,文档没有这么说。它的意思是,如果您在模型上定义
GenericRelation
- 即GenericForeignKey
的反面 - 那么当删除具有通用 FK 的项目时,具有通用 FK 的项目GenericRelation 也将被删除。No, the documentation doesn't say that. What it says is that if you define a
GenericRelation
on a model - ie the reverse side of theGenericForeignKey
- then when the item with the generic FK is deleted, the item with the GenericRelation will also be deleted.我意识到这是一个非常的问题,所以事情可能与提出这个问题时有所不同,但接受的答案让我今天早上追寻了一个兔子洞,因此我想将其留在这里以防止子孙后代分担我的痛苦。
来自文档:
这与公认的答案相反。想象一下:
在上面的示例中,如果您的 Comment 对象有一个指向 Post 对象的通用外键,那么当删除 Post 对象时,指向它的任何 Comment 对象也将被删除。
这是预期的行为,其操作方式与普通外键相同。还是用上面的例子,如果Comment对象指向的User对象被删除,那么Comment也会被删除。
如果您碰巧遇到这个问题,因为您需要这种行为的相反,即当您删除评论时,帖子也会被删除,那么您可能需要使用 信号。
I realise this is a very old question so it's possible things are different from when this was asked, but the accepted answer had me chasing down a rabbit hole this morning, therefore I wanted to leave this here to prevent future generations sharing my pain.
From the docs:
This is the opposite of what the accepted answer is saying. Imagine the following:
In the above example, if your Comment object has a Generic Foreign Key pointing to a Post object, then when the Post object is deleted any Comment objects pointing to it will also be deleted.
This is the expected behaviour and operates the same as a normal ForeignKey. Using the same example above, if the User object that the Comment object points to is deleted, the Comment will also be deleted.
If you happen to chance across this question because you need the reverse of this behaviour, i.e. when you delete the Comment the Post is also deleted, then you will likely need to employ the power of signals.
除了之前的答案之外 - 如果您有更复杂的结构和类似
GenericOneToOne
的东西(在 Django 中不直接存在):并且您想要删除
Post
并成为确保Comment
和Author
也被删除,您需要编写自定义post_delete
信号:如果您重写
delete
方法Comment
类像这样:如果您删除
评论
,它仅会删除作者
。如果删除Post
Author
对象将保留在数据库中。In addition to previous answers - if you have more complex structure and something like
GenericOneToOne
(which is not present in straight way in Django):And you want to delete
Post
and be sure thatComment
andAuthor
are deleted as well you need to write custompost_delete
signal:If you override
delete
method ofComment
class like this:It will delete
Author
only if you deleteComment
. If you deletePost
Author
object will stay in database.