2015-02-09 98 views
0

我有这样一个领域的典范:查找二级孩子的Django MPTT

父= TreeForeignKey( '自我',空=真,空=真, related_name = '孩子')

我想添加家长到选择框,然后与onclick,选择第二级儿童。

parent1 
-section1 
--child1 

parent2 
-section2 
--child2 

我试过所有的东西。 (level__gt),(level__lt)....我已经阅读了django-mptt文档。我如何取第二个孩子?我不想用ul和li。我想添加所有的父母在一个选择,然后获取父亲clik的第二个孩子。

任何帮助,将不胜感激。

回答

3

不确定要完全理解您的问题。这里有一些快捷键:

# 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)