Perhaps you are looking for something like this:
models.py
from django.db import models class Category(models.Model): name = models.CharField(max_length=100) parent = models.ForeignKey('self', blank=True, null=True, related_name='child') def __unicode__(self): return self.name class Meta: verbose_name_plural = 'categories' ordering = ['name']
views.py
from myapp.models import Category
template.html
<ul> {% for cat in cat_list %} <li>{{ cat.name }}</li> <ul> {% for item in cat.child.all %} <li>{{ item.name }}</li> {% endfor %} </ul> {% endfor %} </ul>
Goose
source share