1

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
dman
  • 10,406
  • 18
  • 102
  • 201

1 Answers1

2

You might use itertools.chain(*iterables).

Then, you can create two different querysets and join them together in object_list.

Check this post.

Community
  • 1
  • 1
raratiru
  • 8,748
  • 4
  • 73
  • 113