Asma Gheisari
Asma Gheisari

Reputation: 6254

recursion in view

I want to make this hierarchy:

      <ul id="red" class="treeview-red">
          <li><span>Item 1</span>
            <ul>
              <li><span>Item 1.0</span>
                <ul>
                  <li><span>Item 1.0.0</span></li>
                </ul>
              </li>
              <li><span>Item 1.1</span></li>
            </ul>
          </li>
          <li><span>Item 2</span>
            <ul>
              <li><span>Item 2.0</span>
                <ul>
                  <li><span>Item 2.0.0</span>
                  <ul>
                    <li><span>Item 2.0.0.0</span></li>
                    <li><span>Item 2.0.0.1</span></li>
                  </ul>
                  </li>
                </ul>
              </li>
            </ul>
          </li>
          <li class="open"><span>Item 3</span>
            <ul>
              <li class="open"><span>Item 3.0</span>
                <ul>
                  <li><span>Item 3.0.0</span></li>
                  <li><span>Item 3.0.1</span>
                  <ul>
                    <li><span>Item 3.0.1.0</span></li>
                    <li><span>Item 3.0.1.1</span></li>
                  </ul>

                </li>
                  <li><span>Item 3.0.2</span>
                  <ul>
                    <li><span>Item 3.0.2.0</span></li>
                    <li><span>Item 3.0.2.1</span></li>
                    <li><span>Item 3.0.2.2</span></li>
                  </ul>
                </li>
                </ul>
              </li>
            </ul>
          </li>
        </ul>

with this model:

class Category(models.Model):
    parent=models.ForeignKey('self',null=True,blank=True)
    name=models.CharField(max_length=100)

in views.py I've wrote this simple code to get categories:

def getCats(request):
    cats=Category.objects.all()
    str=u''
    str+='<li><span>Item 1</span><ul>'
    for cat in cats:
        str+=hiararchy(cat)
    str+=u'</ul></li>'
    return HttpResponse(simplejson.dumps(str),mimetype='application/json')

def hiararchy(cat):
    s=''
    if cat.parent is None or cat.parent==cat:
        s=cat.name
    else:
        s=cat.name
        return hiararchy(cat.parent)
    return s

I don't know how to get categories with that format,frankly I'm not very good at recursive functions.

Upvotes: 0

Views: 151

Answers (2)

Asma Gheisari
Asma Gheisari

Reputation: 6254

finally I came up with this solution:

def getCats(request):
    cats=Category.objects.filter(parent=None)
    s=u'<ul id="red" class="treeview-red">'
    for cat in cats:
        s += hiararchy(cat,True)
    s+=u'</ul>'
    return HttpResponse(simplejson.dumps(s),mimetype='application/json')

def hiararchy(cat,flag):
    if cat.parent and flag:
        return hiararchy(cat.parent,True)
    else:
        if cat.children.count()>0:
            s=u'<li><a href="#" id="'+str(cat.pk)+'" class="cat">%s</a>'%(cat.name)
            for child in cat.children.all():
                s+=u'<ul>%s</li></ul></li>'%(hiararchy(child,False))
            return s
        else:
            return u'<li><a href="#" id="'+str(cat.pk)+'" class="cat">%s</a></li>'%(cat.name) 

pay attention : 1)when a category don'e have parent it means it's the root node in tree 2)numbering in my example didn't implement in my code,I needed to replace the name of categories in place of those numbers

Upvotes: 0

Hacking Life
Hacking Life

Reputation: 3373

perhaps you'll find this post useful: I asked a similar question a while back with recursion that looks a bit like yours

Python Recursion through objects and child objects, Print child depth numbers

Upvotes: 1

Related Questions