Reputation: 11064
In Django 1.6, I have a list view. I want to add a couple of properties to each object in object_list. The way I am doing it now, is just over writing the object_list with the last filtered query. How can I add answered_count
and unanswered_count
properties to each object in object_list? For instance:
{% for user in object_list %}
<tr>
<td>{{ user.answered_count }}</td>
<td>{{ user.unanswered_count }}</td>
</tr>
class CommunityProfileListView(LoginRequiredMixin, ListView):
model = CommunityProfile
def get_queryset(self):
qs = super(CommunityProfileListView, self).get_queryset()
qs = qs.filter(threadvault__unanswered=False).annotate(
answered_count=Count('threadvault'))
qs = qs.filter(threadvault__unanswered=True).annotate(
unanswered_count=Count('threadvault'))
return qs
Upvotes: 1
Views: 676
Reputation: 9616
You might use itertools.chain(*iterables).
Then, you can create two different querysets and join them together in object_list.
Check this post.
Upvotes: 2