我在 Django 中有模型、视图和模板,并且想要显示类别的总数。
class Entry (models.Model):
title = models.CharField(max_length=200)
category = models.ForeignKey('entry.Category')
class Category(models.Model):
title = models.CharField(max_length=100)
parent = models.ForeignKey('self', blank=True, null=True, related_name='children')
def category(request):
category = Category.objects.all()
return render_to_response('category.html', locals(), context_instance=RequestContext(request))
<ul>
{% for category in category %}
<li><a href="{{ category.slug }}">{{ category.title }}</a> ({{ category.entry_set.all.count }})</li>
{% endfor %}
</ul>
电流输出:
-类别 1 (0)
--子类别 1 (3)
--子类别 2 (6)
愿望输出是这样的:
-类别 1 (9)
--子类别 1 (3)
--子类别 2 (6)
如何获得该输出?