从查询集中删除项以进行嵌套重组

2024-10-03 21:33:33 发布

您现在位置:Python中文网/ 问答频道 /正文

我一直在做一个项目,将性能作为首要考虑因素,因此我尝试在每个页面使用single-to查询来收集所有需要的信息。在

任何人,我都有一个查询集,需要根据列(左、右、中)重新分组,然后再根据标题重新分组。逻辑运行良好,但当第二次重新组合开始时,它将占用整个查询集,同时我只需要重新组合位于左侧或中间的项目…等等。因此,我搜索函数,从查询集中删除项目,而不触及数据库,我唯一能找到的是建立一个自定义模板,这是我陷入困境的地方:)

这是我的查询结果

    +------------+-------------------+------------+-----------+----+-----------+-----------+---------+----+-------------+-------------+
| col_mapper | list_title        | main_title | list_slug | id | slug      | is_active | site_id | id | domain      | name        |
+------------+-------------------+------------+-----------+----+-----------+-----------+---------+----+-------------+-------------+
| L          | gadget            | for sale   | gadget    |  2 | for-sale  |         1 |       1 |  1 | example.com | example.com |
| L          | furniture         | for sale   | frnture   |  2 | for-sale  |         1 |       1 |  1 | example.com | example.com |
| L          | engines           | for sale   | engines   |  2 | for-sale  |         1 |       1 |  1 | example.com | example.com |
| L          | women seeking men | personals  | wsm       |  1 | personals |         1 |       1 |  1 | example.com | example.com |
| L          | missed connection | personals  | misd-conn |  1 | personals |         1 |       1 |  1 | example.com | example.com |
| L          | men seeking women | personals  | msw       |  1 | personals |         1 |       1 |  1 | example.com | example.com |
| R          | massage           | services   | massage   |  3 | srvces    |         1 |       1 |  1 | example.com | example.com |
| R          | computers         | services   | compters  |  3 | srvces    |         1 |       1 |  1 | example.com | example.com |
+------------+-------------------+------------+-----------+----+-----------+-----------+---------+----+-------------+-------------+

在我的模板中,我做了这样的事情

^{pr2}$

第一个重组工作得很好,但是一旦它到了第二个重组,它会再次重组整个queryset,而我只想在col_mapper等于col的地方重新组合_mapper.gropper. 我试图构建一个自定义标记,但我知道的大多数方法都会导致查询集再次访问数据库进行筛选。在

有什么建议吗?在


Tags: 项目com模板id数据库fortitleexample
1条回答
网友
1楼 · 发布于 2024-10-03 21:33:33

我还没有测试过,但你可以试试:

{% regroup dict by col_mapper as column_gr %}

{% for column in column_gr %}                
    {{ column.grouper }}<br>
    {# assign the grouper to another variable #}
    {% with column.grouper as grouper %}
        {% regroup grouper by main_title as item_gr %}
        {% for i in item_gr %}
            {{ i }}
        {% endfor %}
    {% endwith %}
{% endfor %}

相关问题 更多 >