On Wednesday, 20 November 2013 19:11:26 UTC, Timothy W. Cook wrote: > > I see there was a similar question on this issue earlier today but > alas it didn't answer my question. > > In my app I have models for: > Project: a project (not really part of this question but used for > context in the question). > Paper: an academic paper description > Review: a review of a paper > ... other things not pertinent. > > I can list Projects and from there I can list Papers assigned to each > project. > In the listing of papers I want to show how many reviews there are for > each Paper. > > Paper has a ForeignKey to Project and Review has a ForeignKey to Paper. > > I have this code as my view: > > class PapersListView(ListView): > model = Paper > template_name = 'papers/papers_list.html' > > def get_context_data(self, **kwargs): > context = super(PapersListView, self).get_context_data(**kwargs) > papers = Paper.objects.filter(project__id=self.kwargs['pk']) > review_count = > Review.objects.aggregate(review_count=Count('paper')) > print(review_count) > context['papers'] = papers > context['review_count'] = review_count > return context > > and this in my template: > > <div class="table"> > <table class="listing" cellpadding="2" > cellspacing="2"> > <tr> > <th>Title</th> > <th>Reviews</th> > <th>Year</th> > <th>Journal</th> > <th>Current Stage</th> > </tr> > {% for p in papers %} > <tr> > <td class="style1"><a > href="#">{{p.title}}</a></td> > <td>{{p.review_count}}</td> > <td>{{p.year_published}}</td> > <td>{{p.journal}}</td> > <td>{{p.current_stage}}</td> > </tr> > {% endfor %} > </table> > </div> > > The print() inserted in the view gives me this: > {'review_count': 2} > > In the rendered template the Reviews column is empty. > > There are two Review instances in the database. But, they are for two > separate Papers in two separate Projects. So, my guess is that I have > two problems but after scouring the docs I can't determine where the > mistakes/misunderstandings are. > > Any help is appreciated. > > Cheers, > Tim > > You're accessing a `review_count` attribute via each Paper instance, but you haven't set up anything that would do that - you only have a single top-level `review_count` dictionary.
Instead of a separate `aggregate` call, you should be using `annotate` on the original Paper queryset: papers = Paper.objects.filter(project__id=self.kwargs['pk']).annotate(review_count=Count('review')) Now each element in `papers` has a `review_count` attribute. -- DR. -- 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/d3973b52-ed27-41e9-8daf-e67666f4aaa0%40googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.