我想添加到一个查询集在每个用户的页面请求计算出来的一些统计数据显示在一个大型的表。注释方法可能是最好的选择,但我坚持将创建的查询集合并为一个,以便在模板中进行更简单的操作。查询集类型是首选排序数据。django复杂queryset注释
以下是我的申请极其简化的原则。不能触摸模板和模型,因为它显然是我想要的结果。在这个例子中没有实现按列排序的数据。
下面是型号:
class Poll(models.Model):
question = models.CharField(max_length=200, unique=True)
class Vote(models.Model):
poll = models.ForeignKey(Poll)
accept = models.BooleanField()
comment = models.CharField(max_length=200, unique=True)
censored = models.BooleanField()
这里的观点:
def summaryView(request):
…
contexte['poll_list'] = «insert code here»
…
return render_to_response('summary.html, contexte)
这里的模板:
<table>
<thead>
<tr>
<th>
Poll question
</th>
<th>
Number of votes
</th>
<th>
Number of uncensored "yes" votes
</th>
<th>
Number of censored votes
</th>
</th>
</thead>
<tbody>
{% for poll in poll_list %}
<tr>
<td>
{{ poll.question }}
</td>
<td>
{{ poll.numUncensoredYesVotes }}
</td>
<td>
{{ poll.numCensoredVotes }}
</td>
</tr>
{% endfor %}
</tbody>
</table>
的困难是创造的未经审查的 “是” 数票注释。 Count()聚合函数不接受过滤器。
我正在考虑像这样使用数据库,但希望避免这种情况。无论如何,这是最合理的解决方案。 – 2013-05-12 13:23:00