I have a model for comments which uses GenericForeignKey
and a model for posts.
class Comment(models.Model):
content_type = models.ForeignKey(ContentType)
object_id = models.PositiveIntegerField()
content_object = generic.GenericForeignKey()
text = models.TextField()
comments = generic.GenericRelation('self')
class Post(models.Model):
title = models.CharField(max_length=50)
text = models.TextField()
comments = generic.GenericRelation(Comment)
I can get all comments belonging to a post in templates with
{% for comment in post.comments.all %}
{{ comment }}
{% endfor %}
but I need to comment other comments.
I can get 'next level' of comments with
{% for comment in post.comments.all %}
{{ comment }}
{% for sub_comment in comment.comments.all %}
{{ sub_comment }}
{% endfor %}
{% endfor %}
This way I can only get a specified number of levels.
How can I traverse all comments belonging to the post or belonging to other comments which are belonging to the post?
I smell Tree structure here.
For this in Django you can use: django-mptt
It adds several columns to your table and a lot usefull stuff to work with your model as a tree structure.
And it claims to be very efficient