在Django Mptt找到二级儿童

时间:2015-02-09 20:48:53

标签: javascript django django-mptt

我的模型有这样的字段:

  

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

我想将父级添加到选择框,然后使用onclick,选择第二级子级。

parent1
-section1
--child1

parent2
-section2
--child2

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

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分: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)