【发布时间】:2010-04-14 06:04:10
【问题描述】:
我发布我所在城市的折扣优惠。报价模型被传递给模板(每页约 15 个报价)。每个报价都有很多项目(每个项目都有 FK 到它的报价),因此我必须从模板发出大量的数据库请求。
{% for item in offer.1 %}
{{item.descr}}
{{item.start_date}}
{{item.price|floatformat}}
{%if not item.tax_included %}{%trans "Without taxes"%}{%endif%}
<a href="{{item.offer.wwwlink}}" >{%trans "Buy now!"%}</a> </div>
<div class="clear"></div>
{% endfor %}
所以每页大约有 200-400 个 DB 请求,我预计这是不正常的。
在 django 代码中可以使用 select_related 来预填充所需的值,如何减少模板中的请求数量?
【问题讨论】:
标签: django django-models django-templates