在Django 1.6中,我有一个列表视图。我想为object_list中的每个对象添加几个属性。我现在这样做的方法就是用最后一个过滤的查询编写object_list。如何在object_list中的每个对象中添加answered_count
和unanswered_count
属性?例如:
{% 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
答案 0 :(得分:2)