Re: How can I forloop this count() in my template code?

2013-11-20 Thread Pepsodent Cola
Thanks Tom it worked! :)


On Wednesday, November 20, 2013 12:18:23 PM UTC+1, Pepsodent Cola 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)
>
>
>
> 
> How many Publications was each Article in?
>
> {% if articles %}
> 
> 
> Article id
> Article
> Publication
> 
> {% for row in articles %}
> 
> {{ row.id }}
> {{ row.headline }}
> *total*
> 
> {% endfor %}
> 
> {% else %}
> No Articles are available.
> {% endif %}
>
>
> **
> {% if 666sum_publications %}
> 
> {% for row in sum_publications %}
> {{ row }}
> {% endfor %}
> 
> {% else %}
> No sums are available.
> {% endif %}
>
> 
> 
>
>
>

-- 
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/6a99dfe0-15af-48e2-ae32-498dcf782d6c%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


Re: How can I forloop this count() in my template code?

2013-11-20 Thread Tom Evans
On Wed, Nov 20, 2013 at 11:18 AM, Pepsodent Cola
 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.