How can i create a cycle of comments and replies?

I wonder how can I make a cycle of comments and replies : I wanted to make a replyable comment but replies also need to be replyable to make a better communication but I'm just a student and don't have much experience here is my models :

class Comment(models.Model):
    #comments model
    post = models.ForeignKey(Post, on_delete=models.CASCADE)
    text = models.CharField(max_length=300)
    user = models.ForeignKey(get_user_model(),on_delete=models.CASCADE)
    date = models.DateTimeField(auto_now_add=True)

    class Meta():
        verbose_name_plural = 'Comments'
        ordering = ['date']
    
    def __str__(self):
        return self.test[:50]

class Reply(models.Model):
    #replying to comments
    comment = models.ForeignKey(Comment,on_delete=models.CASCADE)
    text = models.CharField(max_length=300)
    user = models.ForeignKey(get_user_model(),on_delete=models.CASCADE)
    date = models.DateTimeField(auto_now_add=True)

    class Meta():
        verbose_name_plural = 'Replies'
        ordering = ['date']
    
    def __str__(self):
        return self.text[:50]

problem is that if i use this models i have to make a new model for every reply and it's not in cycle. also I tried to check if replyable comment works or not and i got a problem with view: I couldn't add both forms(comment, reply) in the same get_context_data()

class PostDetailView(FormView, DetailView):
    #detail page of items
    template_name = 'pages/post_detail.html'
    model = Post
    form_class = CommentForm, ReplyForm

    def get_context_data(self, **kwargs):
        
        context = super(PostDetailView, self).get_context_data(**kwargs)
        context['form'] = self.get_form()
        return context

    def post(self,request, *args, **kwargs):
        form = CommentForm(request.POST)
        if form.is_valid():
            form_instance = form.save(commit=False)
            form_instance.user = self.request.user
            form_instance.post = self.get_object()
            form_instance.save()
            return HttpResponseRedirect(self.get_success_url())
        else:
            super().is_invalid(form)
    
    def get_success_url(self):
        return reverse('pages:post_detail',args=(self.kwargs['pk'],))

how can i fix views and make comments and replies both replyable

Back to Top