I want to make a nested comment model for an android app and I am using Django Rest framework. I have defined two models Post
and Comment
as follow:
class Post(models.Model):
title = models.CharField(max_length=100)
description = models.TextField()
author = models.ForeignKey('CustomUser',on_delete=models.CASCADE, related_name="author")
created_at = models.DateTimeField(auto_now=True, editable=False)
tag = models.ForeignKey('Tag', on_delete=models.CASCADE, blank=True, null=True)
class Comment(models.Model):
p_post = models.ForeignKey('Post', on_delete=models.CASCADE)
description = models.TextField()
author = models.ForeignKey('CustomUser', on_delete=models.CASCADE)
created_at = models.DateTimeField(auto_now=True, editable=False)
p_comment_id = models.ForeignKey('self', blank=True)
is_anon = models.BooleanField(default=False)
What I am having problem is if I want to get the list of comments on app, so, how should I deal with it? I have following points in my head:
- I can not return the complete list of comments, as it will be a very long list
- So, how do I chose to restrict to return top 5 comments along with their top 5 child comments.