I've made a model of comments with a prarent field to store a comment as a child of other one.
My question is how to display it in template
I've made a model of comments with a prarent field to store a comment as a child of other one.
My question is how to display it in template
You can use the related name field to get the child comments . On the template you will be having two for loops. First for loops iterate through each parent comments and the second one iterate through the chillds of that parent element. For eg:for comment in comments :
For child_comment in comment.related_name.all():
Print child_comment
Sorry I've written this on mobile but I hope you can work it out BTW This is from my own app
let's say you wanna make something like Instagram where every nested comment has a reply button they all would appear under elder_most comment.
This is a weird approach but you can also make every nested button's reply button submit to the elder most comment. This approach is when each reply button refers to its real parent
This is just for your understanding
make a model
Comments
Parent : MantToMany(self refername = child null=true)
User: ForeignKey(user)
Text: TextField
Likes: ForeignKey(comments_likes)
Post: ForeignKey(Posts)
I've made a function which gets the oldest comment and saves it in the oldest
Def Eldermost_comment(comment):
If comment.parent is none:
pass
Else:
comment_ = Eldermost_comment(comment.parent)
comment.parent = comment_
call it in presave signal, this would give every nested comment same parent
Queryset = comments.objects.all.filter(post=123456)
and it's so easy
just using referring name get all the child comments.
for comment in Queryset:
if comment.child is none:
#just display comment
else:
#display comment and then
#display comment.child.all()