在 Django Mptt 中查找二级 Children

Find Second Level Children In Django Mptt

我有一个模型有这样的字段:

parent = TreeForeignKey('self', null=True, blank=True, related_name='children')

我想将parent添加到select框,然后用onclick,select第二层children.

parent1
-section1
--child1

parent2
-section2
--child2

我什么都试过了。 (level__gt), (level__lt) ....我已经阅读了django-mptt文档。如何获取第二个 child?我不想使用 ul 和 li。我想在 select 中添加所有 parents,然后通过点击 parents.

获取第二个 children

如有任何帮助,我们将不胜感激。

不确定是否完全理解您的问题。这里有一些快捷方式:

# all 2^ level 
Model.objects.filter(level=1)

# all leafs (any level)
Model.objects.filter(lft=F('rght') - 1)

# the whole tree except the first node
Model.objects.filter(tree_id=1)[1:]

# all the nodes with childs
Model.objects.exclude(tree_id=1)

# all childs of a node
node.get_children()

# the whole tree of a node (from the top to the last child)
Model.objects.filter(tree_id=node.tree_id)