2

我正在使用django-threadedcomments应用程序,并且由于在下面给出的模型中 parent 是线程评论的外键,所以当我删除评论时它也会被删除。但是我不希望这种情况发生。所以我补充说on_delete = models.SET_NULL,但这似乎不起作用。当我删除评论时,父评论仍会被删除。

这是线程注释模型的一部分,其余的都只是方法,所以我跳过了它们

class ThreadedComment(Comment):
    title = models.TextField(_('Title'), blank=True)
    parent = models.ForeignKey('self', null=True, blank=True, default=None,
        related_name='children', verbose_name=_('Parent'), on_delete=models.SET_NULL)
    last_child = models.ForeignKey('self', null=True, blank=True,
        verbose_name=_('Last child'))
    tree_path = models.TextField(_('Tree path'), editable=False,
        db_index=True)

    objects = CommentManager()

有什么我做错了吗?

4

1 回答 1

1

Threaded comments are a textbook example of DELETE CASCADE. You have a hierarchical structure, so if a parent of a comment is deleted, all the children need to be deleted as well. Otherwise, they're orphaned and your hierarchy is broken. In particular with comments, you can't just assign a child comment to a new parent, because they're often contextual and wouldn't make sense out of context from the comment the reply was posted for.

If you take a look at comment sections on websites across the Internet. When comments are threaded and commenters are allowed to delete their comments (in fact most websites don't let you delete your comments), they never actually delete the comment. Instead the content is simply changed to something like "This comment has been deleted". That way, the content is technically gone, satisfying either the user or the moderator, but it still hangs around for the sake of the hierarchy.

于 2012-07-03T15:00:24.430 回答