0

What is the best way to create a nested comment section?

My current Views is setup is

class AddCommentView(CreateView):
    model = Comment
    form_class = CommentForm
    template_name = 'add_comment.html'

    # fields = '__all__'
    def form_valid(self, form):
        form.instance.post_id = self.kwargs['pk']
        return super().form_valid(form)

    success_url = reverse_lazy('home')

This is my current models is..


class Comment(models.Model):
    post = models.ForeignKey(Post, related_name="comments", on_delete=models.CASCADE)
    name = models.CharField(max_length=250)
    body = models.TextField()
    date_added = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return '%s - %s' % (self.post.title, self.name)

Any help on this would be greatly appreciated!

Mak3232
  • 11
  • 3
  • 1
    I usually go with "parent" field which is ForeignKey field to 'self' – PTomasz Sep 08 '22 at 07:35
  • Here you go, with Django mptt you don't really have to do the tree logic yourself. https://stackoverflow.com/questions/41780538/how-to-create-nested-comment-system-with-django-mptt https://django-mptt.readthedocs.io/en/latest/overview.html – nigel239 Sep 08 '22 at 08:23

0 Answers0