# Django template nested loop

**URL:** <https://forum.djangoproject.com/t/django-template-nested-loop/11377>\
**Category:** Using the ORM\
**Created:** [January 4, 2022, 4:46pm UTC](https://forum.djangoproject.com/t/django-template-nested-loop/11377 "2022-01-04T16:46:58Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![avanzsistemas](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/avanzsistemas/32/6934_2.png) [@avanzsistemas](https://forum.djangoproject.com/u/avanzsistemas)\
**Post date:** [January 4, 2022, 4:46pm UTC](https://forum.djangoproject.com/t/django-template-nested-loop/11377/1 "2022-01-04T16:46:58Z")

</div>

Hi!

I have this model:

```auto
class Category(models.Model):
    name = models.CharField(max_length=255)
    parent = models.ForeignKey('self', on_delete=models.DO_NOTHING, blank=True, null=True, related_name='children')

    def __str__ (self):
        return self.name

```

What i want to do is build a tree with categories that have children, for example

Category1  
Category1-1  
Category1-1-1  
Category2  
Category2-2

But i want to nested categories until categories that have no children and after that show products related to that category

Is this possible?

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [January 4, 2022, 5:23pm UTC](https://forum.djangoproject.com/t/django-template-nested-loop/11377/2 "2022-01-04T17:23:43Z")

</div>

Yes it is possible.

However, there’s a lot more to this topic. If you’re working with any kind of tree structure implemented in a relational database, you will want to consider a more robust implementation of your models.

At a minimum, you’ll want to look at django-treebeard, django-mptt, and django-treenode.

Also see the texts and links at:

- [Django-mptt or treebeard for reddit-like comments?](https://forum.djangoproject.com/t/django-mptt-or-treebeard-for-reddit-like-comments/2030)
- [Some modelling advice - #12 by KenWhitesell](https://forum.djangoproject.com/t/some-modelling-advice/3284/12)
- [tree traversal in template or view?](https://forum.djangoproject.com/t/tree-traversal-in-template-or-view/3473)
- [Multi-level access with ManyToMany relationships in Django](https://forum.djangoproject.com/t/multi-level-access-with-manytomany-relationships-in-django/10783)

---

<div class="post-metadata">

**Author:** ![avanzsistemas](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/avanzsistemas/32/6934_2.png) [@avanzsistemas](https://forum.djangoproject.com/u/avanzsistemas)\
**Post date:** [January 7, 2022, 12:59am UTC](https://forum.djangoproject.com/t/django-template-nested-loop/11377/3 "2022-01-07T00:59:43Z")

</div>

Thanks!

I was reading about django-treebeard

Can you explain me how i can do what i want using it?

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [January 7, 2022, 1:49am UTC](https://forum.djangoproject.com/t/django-template-nested-loop/11377/4 "2022-01-07T01:49:46Z")

</div>

I’d need you to be a lot more specific of what _exactly_ it is you’re trying to accomplish.

---

<div class="post-metadata">

**Author:** ![avanzsistemas](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/avanzsistemas/32/6934_2.png) [@avanzsistemas](https://forum.djangoproject.com/u/avanzsistemas)\
**Post date:** [January 7, 2022, 2:21am UTC](https://forum.djangoproject.com/t/django-template-nested-loop/11377/5 "2022-01-07T02:21:10Z")

</div>

I’ve products and each product has a category.

Now, the categories could’ve a parent, so i want to build a tree with the main categories (categories with no parents) and their children (every children could’ve children too)

So the models with django-treebeard is:

```auto
class Category(MP_Node):
    name = models.CharField(max_length=255)
    parent = models.ForeignKey('self', on_delete=models.DO_NOTHING, blank=True, null=True, related_name='children')

```

So how i can build a tree in my django template with the categories?

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [January 7, 2022, 3:58am UTC](https://forum.djangoproject.com/t/django-template-nested-loop/11377/6 "2022-01-07T03:58:28Z")

</div>

I need you to provide more details. What _precisely_ do you mean by “build a tree”? Do you have an idea of the html you’re trying to generate?

---

<div class="post-metadata">

**Author:** ![avanzsistemas](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/avanzsistemas/32/6934_2.png) [@avanzsistemas](https://forum.djangoproject.com/u/avanzsistemas)\
**Post date:** [January 7, 2022, 1:19pm UTC](https://forum.djangoproject.com/t/django-template-nested-loop/11377/7 "2022-01-07T13:19:50Z")

</div>

I wanna make something with

 and -

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [January 7, 2022, 1:53pm UTC](https://forum.djangoproject.com/t/django-template-nested-loop/11377/8 "2022-01-07T13:53:47Z")

</div>

Cool. So it’s a depth-first rendering of a tree.

First, since you’re using an MP\_Node, you no longer need a separate parent field.

Given an instance of `Category` named `category`, `category.get_parent()` retrieves the parent of that node, and `category.get_children()` retrieves the child nodes.

Rendering this as a tree structure can be done using a recursive use of a template.

For example, you could have a `render_nodes.html` template that looks something like this:

```auto
<li>{{ category.name }}
<ul>
{% for children in category.get_children %}
{% include "render_nodes.html" with category=children only %}
{% endfor %}
</ul>
</li>

```

which is then started in your parent template with something like:

```auto
<ul>
{% include "render_nodes.html" with category=root_node only %}
</ul>

```

(Obviously this is illustrative and not definitive.)

Now, as a side note - if this is going to be something rendered frequently, like a menu, you are **not** going to want to generate this with every request. As written, this is going to generate N+1 queries. You will want to do some sort of caching on that result.
