2015-02-09 45 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)