This should be simple, but I'm shredding my hair just trying to think how to tackle it!
I have a navigation menu down the side of my site that is used to pick products. It's formatted like so:
- Every product belongs in a 'list' (clicking on the list sends to you a page with the list of products).
- Every list belongs in a 'category' (clicking on the category uses jquery to expand out a list beneath it when clicked, but that's besides the point)
- Every Category belongs in a 'Category Group' that separates the categories into distinct groups depending on what area of the business you're dealing with)
In simpler terms, products belong in a list, which belong in a category, which belongs in a category group. They are all one-to-many relationships.
I need to pass this from my view to my template in such a way as I can render a nested list in HTML for jquery to make pretty. Something like:
<ul>
<li>Category Group</li>
<ul>
<li>Category</li>
<ul>
<li>List</li>
<li>List</li>
<li>List</li>
</ul>
<li>Category</li>
<ul>
<li>List</li>
<li>List</li>
<li>List</li>
</ul>
</ul>
<li>Category Group</li>
<ul>
<li>Category</li>
<ul>
<li>List</li>
<li>List</li>
<li>List</li>
</ul>
<li>Category</li>
<ul>
<li>List</li>
<li>List</li>
<li>List</li>
</ul>
</ul>
</ul>
My issue is creating said hierarchical list to pass to the template so it can render that. I'm aware that I need to use Model.FK_set.all() to get say, a list of 'categories' in a 'category group', but I can't figure out how to create that list in the view in an appropriate way to send to the template. Any help? Python newbie, so still learning the ropes.