【发布时间】:2020-10-04 22:28:51
【问题描述】:
我需要在我的项目中使用 REST API 构建一个简单的嵌套评论系统。
有一些要求:
该页面应包含所有 cmets 的分页,而不仅仅是父 cmets。因此,如果有 1 条评论和 20 条来自其他用户的嵌套 cmets,则应在第一页显示,如下所示:
-评论1
-- 子评论1
-- ...
-- 子评论9
在第二页:
-- 子评论10
-- ...
-- 子评论 19
在第三页:
-- 子评论 20
就最少的数据库访问而言,它应该是最佳解决方案。
我想知道自定义 Queryset 排序,当每个评论后跟嵌套的 cmets 之后,就像这样:
// 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
},
我也想过和谷歌有关 Django-mptt,但不知道如何实现它。您能建议我如何解决这个问题并像我的示例中那样获取 JSON 吗?
【问题讨论】:
标签: python django django-rest-framework django-queryset django-mptt