【发布时间】:2018-01-09 07:19:17
【问题描述】:
有 2 个模型
class Category(MPTTModel):
title = models.CharField()
parent = TreeForeignKey('self', null=True, blank=True, related_name='children', db_index=True)
class Product(models.Model):
category = models.ForeignKey(Category, blank=True, null=True)
我想从两个模型中获取树。 现在我可以得到:
在视图中
nodes = Category.objects.all()
在模板中
{% for n in nodes %}
{% for t in n.product_set.all %}
但我想得到
nodes = Category.objects.all().**insert_to_category_product_with_filter**(id__in=[list_id])
【问题讨论】: