0

我有两个模型。评论和他的“子评论”:

class Comment(models.Model):

    ....
    author = models.CharField(max_length=80)
    published = models.DateTimeField(auto_now_add=True)
    email = models.EmailField(blank=True)
    url = models.URLField(blank=True)
    post = models.ForeignKey(Entry)
    subcomments = models.ManyToManyField('Subcomment', blank=True)
    ....


class Subcomment(models.Model):

    ....
    author = models.CharField(max_length=80)
    published = models.DateTimeField(auto_now_add=True)
    email = models.EmailField(blank=True)
    url = models.URLField(blank=True)
    mcomment = models.ForeignKey(Comment)
    ....

我试图订阅 RSS 来发表评论。我使用以下代码:

class EntryCommentsFeed(Feed):

    ....
    def items(self, obj):
        return Comment.not_spam.filter(post=obj).order_by('-published')[:15]
    ....

但它只返回没有子评论的评论,我不知道如何用他的“子评论”返回评论本身并按日期排序。

4

1 回答 1

0

这是不可能的。模型查询集仅由该模型类型的对象组成。但是,您可以遍历返回Comment的 s 并获取Subcomment每个的 s:

for comment in Comment.not_spam.filter(post=obj).order_by('-published')[:15]:
    subcomments = comment.subcomment_set.all()
于 2011-06-02T19:18:28.343 回答