你如何排序django mptt树? [英] How would you sort a django mptt tree?

查看:119
本文介绍了你如何排序django mptt树?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想象一下,我有一个对象和他们的人群的mptt树,如:

Imagine that I have an mptt tree of objects and their population like:

动物,60


  • aardvark,30

  • bobcat,20

  • 花栗鼠,10

蔬菜,6


  • apple,1

  • 甜菜,2

  • 花椰菜,3

矿物0

您如何按照每个子级别的人口对以上内容进行排序?我想去:

How would you sort the above by population on each sublevel? I want to get to:

动物,60


  • aardvark,30

  • bobcat,20

  • 花栗鼠,10

蔬菜,6


  • 花椰菜,3

  • 甜菜,2

  • apple,1

矿物0

我正在建立在django中的mptt。

I am building off of mptt in django.

推荐答案

我刚刚解决了类似的问题。您可以使用 order_by ,但不能简单地按您要排序的字段:

I've just solved a similar issue. You can use an order_by, but not simply by the field you want to sort by:

MyModel.tree.all().order_by('tree_id', 'level', 'your_sort_field')

这篇关于你如何排序django mptt树?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆