3

无论出于何种原因,我都无法在本地复制此问题,但在我的一个模板中的生产服务器上,重组标签正在做一些奇怪的事情。我正在尝试对所有具有相同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…&lt;/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>
4

1 回答 1

9

问题是 Django 的重新组合取决于您重新组合所依据的属性排序的列表。请参阅文档

请注意,{% regroup %} 不会对其输入进行排序!我们的示例依赖于这样一个事实,即城市列表首先是按国家/地区排序的。如果城市列表未按国家/地区对其成员进行排序,则重组将天真地显示单个国家/地区的多个组

因此,改变:

missed_routes = Donor.objects.filter(missed='YES').order_by('pickup_date')  

missed_routes = Donor.objects.filter(missed='YES').order_by('pickup_id')  
于 2012-09-17T01:41:27.013 回答