gpt4 book ai didi

django重新组合奇怪的行为

转载 作者:行者123 更新时间:2023-12-04 00:09:12 24 4
gpt4 key购买 nike

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

resulting 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>

最佳答案

问题在于 Django 的重新组合取决于您重新组合所依据的属性对列表进行排序。见 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

因此,改变:

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

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

关于django重新组合奇怪的行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12451961/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com