在 django 中排序查询对象
我试图以某种父/子方式对查询结果进行排序。我想知道是否有一种简单的方法可以实现这一点。
对象:
Video : [id, parent_id, date] // where parent_id can be null - meaning it is a 'root' node.
查询集排序我想要:
Video 1: [123, null, 01/11]
Video 2: [111, 123, 02/11]
Video 3: [144, 123, 04/11]
Video 4: [191, null, 03/11]
Video 5: [118, 191, 03/11]
Video 6: [121, 191, 05/11]
Video 7: [411, null, 04/11]
...
有没有办法在查询集中实现这种父/子组排序?
I'm trying to order the results of my query in a sort of parent/child manner. I'm wondering if there's an easy way to accomplish this.
Object:
Video : [id, parent_id, date] // where parent_id can be null - meaning it is a 'root' node.
Queryset ordering I want:
Video 1: [123, null, 01/11]
Video 2: [111, 123, 02/11]
Video 3: [144, 123, 04/11]
Video 4: [191, null, 03/11]
Video 5: [118, 191, 03/11]
Video 6: [121, 191, 05/11]
Video 7: [411, null, 04/11]
...
Is there a way to achieve this sort of parent/ child group ordering in a queryset?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果需要在模板级别使用此功能,可以使用
regroup
模板标签对查询集重新排序:https://docs.djangoproject.com/en/dev/ref/templates/builtins/?from=olddocs#regroup
If you need to use this at the template level, you can use the
regroup
template tag to reorder the queryset:https://docs.djangoproject.com/en/dev/ref/templates/builtins/?from=olddocs#regroup
你需要使用django-mptt,它将为你解决这个问题。
有关 MPTT 的更多信息,请参见 wikipedia
You need to use django-mptt, which will solve this problem for you.
More information on MPTT from wikipedia