[英]How do I query objects of all children of a node with Django mptt?
I am trying to get the objects of all the children of a given node on Django with django-mppt 我试图用django-mppt获取Django上给定节点的所有子节点的对象
I have a model designed as shown below, the classes/categories (node) with the same indent level defines siblings, inner indents are children. 我有一个如下所示设计的模型,具有相同缩进级别的类/类别(节点)定义兄弟,内部缩进是子。 The objects tagged with a category are shown just below the category (node).
标有类别的对象显示在类别(节点)下方。 The objects start with a
-
symbol. 对象以
-
符号开头。 The numbers along the classes/categories (nodes) are the ids. 类/类别(节点)中的数字是ids。
all the nodes are the instances of Category
class with the id
given. 所有节点都是具有
id
的Category
类的实例。
high school (1)
class 8 (2)
division a (3)
-Billie
-Tre
-Mike
division b (4)
-Patrik
-Pete
-Andy
class 9 (3)
division a (8)
-Mark
-Tom
-Travis
division b (5)
-Gerard
-Frank
-Mikey
class 10 (4)
division a (6)
-Hayley
-Jeremy
-Taylor
division b (7)
-Steven
-Slash
-Izzy
I can get the query sets of a specific node this way, 我可以通过这种方式获取特定节点的查询集,
>>> Category.objects.get(pk=7).product_set.all()
[Steven, Slash, Izzy]
>>> Category.objects.get(pk=4).product_set.all()
[Mark, Tom, Travis]
How do I query with pk=1
, pk=2
, pk=3
or pk=4
to get all the child objects? 如何查询
pk=1
, pk=2
, pk=3
或pk=4
来获取所有子对象?
example, 例,
the query for pk=2
query must return pk=2
查询的查询必须返回
[Billie, Tre, Mike, Patrik, Pete, Andy]
Category.objects.get(pk=2).get_descendants(include_self=True)
This will get you all category descendants including self. 这将为您提供包括自我在内的所有类别后代。
Assuming that your Product model has a Foreign key category, you can use: 假设您的产品型号具有外键类别,您可以使用:
Product.objects.filter(category__in=Category.objects.get(pk=2)\
.get_descendants(include_self=True))
Django mptt provides two methods to retrieve children. Django mptt提供了两种检索子项的方法。
MPTTModel.get_children(*args, **kwargs)
MPTTModel.get_children(* args,** kwargs)
Returns a QuerySet containing the immediate children of this model >instance, in tree order.
以树顺序返回包含此模型>实例的直接子项的QuerySet。
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).
使用此方法优于ORM为实例的子项提供的反向关系的好处是,在实例是叶子节点(它没有子节点)的情况下,可以避免数据库查询。
If called from a template where the tree has been walked by the cache_tree_children filter, no database query is required.
如果从已通过cache_tree_children过滤器遍历树的模板调用,则不需要数据库查询。
And 和
MPTTModel.get_leafnodes(*args, **kwargs)
MPTTModel.get_leafnodes(* args,** kwargs)
Creates a QuerySet containing leafnodes of this model instance, in tree order.
按树顺序创建包含此模型实例的叶节点的QuerySet。
If include_self is True, the QuerySet will also include this model instance (if it is a leaf node)
如果include_self为True,则QuerySet还将包含此模型实例(如果它是叶节点)
I'm not sure how your models are set up but I'm not sure why you use mptt here. 我不确定你的模型是如何设置的,但我不确定你为什么在这里使用mptt。 You are using Category/Product but it seems to be student or people and work groups.
您正在使用类别/产品,但它似乎是学生,人员和工作组。
Maybe you can define EstablishmentLevel
, Level|
也许你可以定义
EstablishmentLevel
, Level|
, StudentGroup
, Student
models and instead of using mptt function query something like: ,
StudentGroup
, Student
模型,而不是使用mptt函数查询,如:
Student.objects.filter(studentgroup__level__pk=1)
See the Django doc 请参阅Django文档
Hope that helped 希望有所帮助
Category.objects.get(pk=1).get_leafnodes()
is what you're looking for. Category.objects.get(pk=1).get_leafnodes()
就是你要找的东西。
( django-mptt docs ) ( django-mptt docs )
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.