On Wed, Nov 20, 2013 at 11:18 AM, Pepsodent Cola <pepsodentc...@gmail.com> wrote: > How can I forloop this count() in my template code? > > * I have attached my project files that you might run and see for > yourselves. > * I have attached a screenshot of the table I'm working on. > * User/Pass = joe/joe > > > def index(request): > publications = Publication.objects.all() > articles = Article.objects.all() > > sum_publications = Publication.objects.filter(article__pk=1).count() > > context = {'publications':publications, 'articles':articles, > 'sum_publications':sum_publications} > return render(request, 'magazine/index.html', context)
Are you trying to annotate each row in the 'articles' queryset with the count of the number of publications associated with that article? from django.db.models import Count articles = Article.objects.all().annotate(num_publications=Count('publication')) {% for article in articles %} {{ article.id }} {{ article.num_publications }} {% endfor %} Cheers Tom -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at http://groups.google.com/group/django-users. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAFHbX1L33SMPVHFZZNnuy__sSPb-iy7BKUuVEVWg%2BTNkkyqa2A%40mail.gmail.com. For more options, visit https://groups.google.com/groups/opt_out.