0

I have two models. Comment and his "Subcomments":

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)
    ....

I trying to make RSS subscribtion to post comments. I use following code:

class EntryCommentsFeed(Feed):

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

But It returns only Comments without subcomments, and i don't have any idea how to return comment itself with his 'subcomments' and order by date.

bosha
  • 69
  • 1
  • 10

1 Answers1

0

It's not possible. Model querysets are only ever composed of objects of that model type. You can loop through the returned Comments and get the Subcomments for each, though:

for comment in Comment.not_spam.filter(post=obj).order_by('-published')[:15]:
    subcomments = comment.subcomment_set.all()
Chris Pratt
  • 232,153
  • 36
  • 385
  • 444
  • Yeah, this works. I hoped have a better solution. Thanks for reply =] – bosha Jun 03 '11 at 16:48
  • Strange.. I defined the function - http://pastie.org/private/6oo8ttmawyfrkd21ouyhq , but django return error - http://pastie.org/private/z59dl06elgsafwnpx4wlfa . Strange thing is that, both models have get_absolute_url.. – bosha Jun 03 '11 at 19:42
  • Not so strange. The feeds app is expecting a single iterative queryset. You passed it a list of querysets. That's not going to work. Use `itertools.chain` to combine the two querysets (http://docs.python.org/library/itertools.html#itertools.chain). Realize, though, there's not going to be any hierarchy to your feed, i.e. `Subcomment`s are not going to be associated with `Comment`s. Everything will just be inline. – Chris Pratt Jun 03 '11 at 21:06
  • Fount that topic - http://stackoverflow.com/questions/431628/how-to-combine-2-or-more-querysets-in-a-django-view Trying this, but have a new error `1242, 'Subquery returns more than 1 row'`. Google near six hours on a solution, but i didn't find it. I think i should learn pure SQL and "delve" into the django ORM before i can do this. Thank you for help =) – bosha Jun 05 '11 at 09:26