views:

28

answers:

1

I'm looking to categorize my entries, the catch is there are multiple levels of categories I want. An example:

css
    layout
          floats
    specificity, selectors
html
    html 5

In this example, css and html are parent categories, css has 2 children and layout has a child of floats.

I suppose the schema I would need would be

class Category:
    name = models.TextField()
    parentId = models.IntegerField(blank=True)

What I'm clueless on is, how would I be able to make a multi-level dropdown in my admin so that when I post entries I could select a category easily?

So to reiterate, how would I be able to generate a multi-level nested dropdown menu so that when I enter stuff in my Entry model, I can select one category per entry?

+2  A: 

It seems that your problem is slightly different from what you are stating. The issue here is not that much about how to display the hierarchy, which is simple:

def __unicode__(self):  
    return self.depth * " "

The bummer is how to capture and display the hierarchy / depth. This is a common problem: storing trees in realational databases. As usual, your solution depends on tradeoffs between write / read heavy and how much to normalize. You could, for example, on the 'save' method of the model to recursively get to the root and from there store a 'depth' attribute on the nodes. My suggestion is to use django mptt . It is pretty solid and solves much of the normal hurdles. As a bonus, you get a good api for common tree tasks.

Arthur Debert