我出于某种原因无法在本地复制此问题,但在我的一个模板中的生产服务器上,regroup标签正在做一些奇怪的事情。我正在尝试将所有具有相同pickup_id的捐款分组,并显示其中有多少是“错过”。出于某种原因,它在我的列表中不止一次显示相同的pickup_id。
视图
missed_routes = Donor.objects.filter(missed='YES').order_by('pickup_date')
模板
{% block content %}
{% regroup missed_routes by pickup_id as missed_pickups %}
<div class="missedColumn">
<h2>Missed Donations</h2>
<p>Looks like we have some stragglers…</p>
<p>These routes have missed donations in them.</p>
{% for routes in missed_pickups %}
<p><a href="/reschedule/{{ routes.grouper }}">{{ routes.list.0.route }}</a> - {{ routes.list.0.pickup_date }} ({{ routes.list|length }} missed - {{ routes.list.0.pickup_id }}</p>
{% endfor %}
</div>
结果html#600是pickup_id
<p><a href="/reschedule/600">Syosset</a> - Sept. 14, 2012 (1 missed - 600</p>
<p><a href="/reschedule/423">Huntington Station</a> - Sept. 14, 2012 (1 missed - 423</p>
<p><a href="/reschedule/600">Syosset</a> - Sept. 14, 2012 (2 missed - 600</p>
在拾取html输出中迭代每个捐赠者时
<p><a href="/reschedule/600">Syosset</a> - Sept. 14, 2012 (1 missed - 600
<ul>
<li>134170</li>
</ul>
</p>
<p><a href="/reschedule/423">Huntington Station</a> - Sept. 14, 2012 (1 missed - 423
<ul>
<li>134938</li>
</ul>
</p>
<p><a href="/reschedule/600">Syosset</a> - Sept. 14, 2012 (2 missed - 600
<ul>
<li>134174</li>
<li>133151</li>
</ul>
答案 0 :(得分:4)
问题在于Django的重新组合取决于您正在重新组合的属性所排序的列表。请参阅docs:
请注意{%regroup%}不会对其输入进行排序!我们的例子依赖于城市名单首先按国家排序的事实。如果城市列表没有按国家/地区对其成员进行排序,则重新组合将天真地为单个国家/地区显示多个组
因此,改变:
missed_routes = Donor.objects.filter(missed='YES').order_by('pickup_date')
到
missed_routes = Donor.objects.filter(missed='YES').order_by('pickup_id')