Search code examples
djangodjango-comments

Django comments reverse relationship


When using django.contrib.comments is there anyway to add the reverse relationship to a model that has comments?

For example:

post = Post.objects.all()[0]
comments = post.comments.all()

Solution

  • Yes, you should be able to do:

    from django.contrib.contenttypes import generic
    class Post(models.Model):
        ...
        comments = generic.GenericRelation(Comments)
    

    per the Django docs on reverse generic relations