I need to build a simple nested comment system in my project with REST API.
There are some requirements:
The page should contain the pagination of all comments, not just the parent ones. So, if there are 1 comment and 20 nested comments from other users, then on the first page should be displayed, like so:
-Comment 1
-- Sub comment 1
-- ...
-- Sub comment 9
On the second page:
-- Sub comment 10
-- ...
-- Sub comment 19
On the third page:
-- Sub comment 20
And it should be an optimal solution in terms of the least database access.
I was wondering about custom Queryset ordering, when after each comment follows nested comments, like so:
// Note, that ids follow in chaotic order
{
id: 1,
text: "Comment 1",
parent: null
},
{
id: 5,
text: "Sub comment 1 for Comment 1",
parent: 1
},
{
id: 6,
text: "Sub comment 2 for Comment 1",
parent: 1
},
{
id: 2,
text: "Comment 2",
parent: null
},
{
id: 3,
text: "Sub comment 1 for Comment 2",
parent: 2
},
{
id: 4,
text: "Sub comment 2 for Comment 2",
parent: 2
},
I've also thought and google about Django-mptt, but don't know how to implement it. Can you suggest to me, how to solve this problem and get JSON like in my example?