I for whatever reason cannot duplicate this issue locally but on my production server in one of my templates the regroup tag is doing something odd. I'm trying to group all donations that have the same pickup_id and just display how many of them were "missed". For some reason it's showing the same pickup_id more than once in my list.
view
missed_routes = Donor.objects.filter(missed='YES').order_by('pickup_date')
template
{% 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>
resulting html # the 600 is the 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>
When iterating over each donor in a pickup html output
<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>
The problem is that Django's regroup depends on the list being ordered by the attribute you're regrouping by. See the docs:
Note that {% regroup %} does not order its input! Our example relies on the fact that the cities list was ordered by country in the first place. If the cities list did not order its members by country, the regrouping would naively display more than one group for a single country
Thus, change:
missed_routes = Donor.objects.filter(missed='YES').order_by('pickup_date')
to
missed_routes = Donor.objects.filter(missed='YES').order_by('pickup_id')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With