<p>Django mptt提供了两种检索子级的方法。</p>
<p>从<a href="http://django-mptt.github.io/django-mptt/mptt.models.html?highlight=child#mptt.models.MPTTModel.get_children" rel="nofollow">docs</a></p>
<blockquote>
<p>MPTTModel.get_children(*args, **kwargs)</p>
<p>Returns a QuerySet containing the immediate children of this model >instance, in tree order.</p>
<p>The benefit of using this method over the reverse relation provided by the ORM to the instance’s children is that a database query can be avoided in the case where the instance is a leaf node (it has no children).</p>
<p>If called from a template where the tree has been walked by the cache_tree_children filter, no database query is required.</p>
</blockquote>
<p>以及</p>
<blockquote>
<p>MPTTModel.get_leafnodes(*args, **kwargs)</p>
<p>Creates a QuerySet containing leafnodes of this model instance, in tree order.</p>
<p>If include_self is True, the QuerySet will also include this model instance (if it is a leaf node)</p>
</blockquote>
<p>我不知道你的模型是如何建立的,但我不知道你为什么在这里使用mptt。您使用的是类别/产品,但似乎是学生或人员和工作组。</p>
<p>也许您可以定义<code>EstablishmentLevel</code>、<code>Level|</code>、<code>StudentGroup</code>、<code>Student</code>模型,而不是使用mptt函数查询,例如:</p>
<pre><code>Student.objects.filter(studentgroup__level__pk=1)
</code></pre>
<p>见<a href="https://docs.djangoproject.com/en/1.8/topics/db/queries/" rel="nofollow"> Django doc </a></p>
<p>希望有帮助</p>