我有Category
这样的模型:
class Category(models.Model):
name = models.CharField(max_length=200)
slug = models.SlugField(unique=True)
parent = models.ForeignKey('self', blank=True, null=True, related_name="children")
description = HTMLField(blank=True, null=True)
我希望将它们列为这样:
导航栏中没有父级的类别如下:
Category.objects.filter(parent=None)
让我们说父== SuperParent。
现在我想在小部件中加载其他类似父类的类别:
Widget Box 1:
Category Parent (children of SuperParent):
Child1 of parent
Child2 of parent
.. and so on
Widget Box 2
Category Parent(another name)(Children of SuperParent):
Childs
怎么做?
由于
答案 0 :(得分:0)
p_cat = Category.objects.filter(parent = None)
<强>模板强>
{%for cat in p_cat%}
{{cat.name}}
{%表示cat.children.all%中的ch_cat}
- {{ch_cat.name}}
{%endfor%}
{%endfor%}