title: Tree Structure (Foreign Keys to itself) and templates
body: <p>I have a tree structure for Categories. Categories with a Foreign key that is referring to itself.</p>
  
  <pre><code>class Category(MetaData):
      parent = models.ForeignKey('self', blank=True, null=True, verbose_name='parent category', on_delete=models.CASCADE)
      name = models.CharField(max_length=255)
      description = models.TextField()
  </code></pre>
  
  <p>Because I don't know the depth of the categories tree(can't use for) I need to use a recursive function:</p>
  
  <pre><code>def cat_for_parents(self, cat_obj):
                  ...
                  if cat_obj.parent_id:
                          p = cat_obj.parent
                          ...
                          self.cat_for_parents(p)
  </code></pre>
  
  <p>But how I implement the function in the template to get something like this(to theoretically infinite recursive loop):</p>
  
  <pre><code>&lt;ul&gt;
   &lt;li&gt;CategoryA
    &lt;ul&gt;
     &lt;li&gt;SubCategA1
       &lt;ul&gt;
         &lt;li&gt; Subcateg Subcateg B1
           &lt;ul&gt;
              &lt;li&gt; Subcateg SubCateg C1&gt;
               &lt;li&gt; Subcateg SubCateg C2&gt;
           &lt;ul&gt;
           &lt;li&gt; Subcateg Subcateg B2&gt;
           ............. 
  </code></pre>