Django,类别和子类别

使用DataModel即时工作类别和子类别,这部分都很好,但我需要在我的菜单导航中使用我的类别和子类别,我尝试使用这个Jquery菜单 ,并且我使用子类别渲染我的菜单,但我迷失了以这种方式呈现子类别:

  • Category Subcategory 1 Subcategory 2 ...
  • .... ....

我的问题:在数据模型中:使用’self’,我不知道在这种情况下做一个子类别(父亲是自己的字段)是多么糟糕…

 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) 

然后:

 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 

如果你有多个级别类别,那么在那里需要进行递归调用,但这给出了基本的要点。