2011-04-15 72 views
0

我一直在考虑将性能作为最高优先级的项目,因此我试图在每个页面上使用单个查询来收集所有需要的信息。从查询集中删除项目以进行嵌套重组

任何人,我有一个点,我有一个查询设置,需要根据列(左,右,中心)重新分组,然后再根据标题重新组合。逻辑工作正常,但是当第二个重新组合开始时,它将占用整个查询集,同时我只需要重新组合左侧或中间的项目..等等。所以,我搜索功能删除从查询集项目而无需访问数据库,只有我能找到的东西是要建立这是我卡住了:)

这是我的查询结果的自定义模板

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

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

  {% regroup dict by col_mapper as column_gr %} 

     {% for column in column_gr %}     
      {{ column.grouper }}<br> 

      {% regroup column.list by main_title as item_gr %} 
       {% for i in item_gr %} 
        {{ i }} 
       {% endfor %} 
     {% endfor %} 

第一个重新组合工作正常,但一旦它到达第二个重新组合,而我只想重新集结,其中col_mapper等于再次重组了整个查询集col_mapper.grouper。我试图构建一个自定义标记,但大多数我知道的方法会导致queryset再次点击数据库进行过滤。

有什么建议吗?

回答

1

我没有测试过这一点,但你可以尝试:

{% 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 %} 
+0

感谢队友,它的工作。我不得不添加一个额外的循环..但它的作品完美:) – 2011-04-15 14:31:15

+0

太棒了!很高兴我能帮上忙。 – Brandon 2011-04-15 14:31:46