2009-09-15 53 views
1

进出口工作类别和子类别与DataModel,都是在这部分罚款,但我需要用我的类别和子类别在我的菜单导航,即时通讯尝试使用这个Jquery menu,并即时渲染我的菜单与子类别,但即时通讯失去的方式呈现子类别:Django的,类别和子类别

<ul> 
    <li> 
    <a href="#">Category</a> 

    <!--subcategories--> 
    <span>Subcategory 1 </span> 
    <span>Subcategory 2 </span> 
    ... 
    </li> 
    .... 
    .... 
</ul> 

我的问题:在数据模型:与“自我”,我不知道如何生病在这种情况下做一个为使子类(父亲是该领域本身)。

class Category(models.Model): 
name = models.CharField(core=True, maxlength=200) 
slug = models.SlugField(prepopulate_from=('name',)) 
parent = models.ForeignKey('self', blank=True, null=True, related_name='child') 
description = models.TextField(blank=True,help_text="Optional") 

感谢使用类似

top_level_cats = Category.objects.filter(parent__isnull=True) 

然后

回答

2

获取所有顶级类别:

for tlc in top_level_cats: 
    #do the HTML for the top-level category 
    for clc in tlc.child.all(): 
     #do the HTML for the children of clc 

如果您有多个层次的类别,有需要是一个递归调用在某处,但这给出了基本的要点。

+0

谢谢,正在工作:D – Asinox 2009-09-16 00:23:20