Answer the question
In order to leave comments, you need to log in
How to display nested categories?
There is a model
class Category(models.Model):
name = models.CharField('Наименование', max_length=100)
slug = models.SlugField(max_length=160, unique=True)
parent = models.ForeignKey(
'self', verbose_name='Родитель', on_delete=models.SET_NULL, blank=True, null=True, related_name='children'
)
@register.simple_tag()
def get_categories():
return Category.objects.all()
.filter(parent__isnull=True)
, but how then to display child categories?{% get_categories as categories %}
{% for category in categories %}
<a>{ category.name }</a>
{% endfor %}
Answer the question
In order to leave comments, you need to log in
Recursively. Google django-mptt and either use or steal the implementation from there
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question