remove items from queryset for nested regrouping
I've been working on a project that t开发者_运维问答akes into consideration performance as the top priority therefore am trying to use single to queries at each page to collect all the needed information.
Any who, I got to a point where I have one query set that need to be regrouped based on column (left, right, center) and then again regrouped based on title. The logic is working fine, but when the second regroup starts it will take the entire queryset meanwhile I only need to regroup items that are on the left or center..etc. so, I searched for functions to remove items from the queryset without hitting the database and only thing I could find was to build a custom template which is where I got stuck :)
This is my query result
+------------+-------------------+------------+-----------+----+-----------+-----------+---------+----+-------------+-------------+
| 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 |
+------------+-------------------+------------+-----------+----+-----------+-----------+---------+----+-------------+-------------+
In my template, I did something like this
{% 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 %}
The first regroup is working fine, but once it gets to the second regroup it regroups again the whole queryset while I want to only regroup where col_mapper is equal to col_mapper.grouper. I tried to build a custom tag, but most the approaches I know they will cause the queryset to hit the database again for filtering.
Any suggestions?
I haven't tested this, but you might try:
{% 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 %}
精彩评论