Marcio Cruz
Marcio Cruz

Reputation: 2069

How do I make nested regroups in Django?

I've got the following situation in this system: Each category of products has many subcategories, and each subcategory has many products under it. I'm trying to make a product searh, which returns a list, and in my template, I show an overview of the results, like this:

Cellphones
    Dumbphones (2 results)
    Smartphones (3 results)
Monitors
    CRT (1 result)
    LCD (3 results)

I'm my template I have only the list of products. I've tryed many combinations of nested regroups, without success. Any ideas?

Upvotes: 3

Views: 648

Answers (1)

chehov
chehov

Reputation: 163

You can try something like this:

<div>    
...
{% regroup results|dictsort:"subcategory.category" by subcategory.category as categories %}
<ul>
    {% for category in categories %}
    <li>{{ category.grouper }}
        {% regroup category.list|dictsort:"subcategory" by subcategory as subcategories %}
        <ul>
            {% for subcategory in subcategories %}
                <li>{{ subcategory.grouper }}
                    <!--The same way you can render a subcategory.list which is the prosucts list-->
                </li>
            {% endfor %}
        </ul>
    </li>
    {% endfor %}
</ul>
acceptance_report.company as companies %}
...
</div>

Upvotes: 2

Related Questions