tags:

views:

31

answers:

1

I'm writing entries for cross-browser bugs/workarounds. I would like the main view to present a hierarchial tree starting with the primary categories and descending into more specific categories:

css
    layout
       float
       position
    specificity

js
    dom
html
    object
    embed

Let's say I want to file an entry and have it show up under both float and position because the bug in this entry is a combination of the two.

My category schema is as such:

category_id    category_name    parent_id

With that, my float row would be:

10   float  9

9 ( parent_id ) points to my layout row:

9    layout 8

8 points to my css row:

8    css  null

Question 1: Since entries can potentially have many categories, I need a table to map those relationships, right? I currently have an Entries and Categories model, so I'd need a third table? It would contain the category_id and the entry_id.

Question 2: How can I preserve a tree/hierarchial view of the categories if I'm doing many categories to one entry? I'm a bit confused because initially it seemed a little easier with one category but since I have multiple I'm confused as to how I would even begin this.

Models so far:

class Bug( models.Model ):
    name = models.CharField( max_length=100 )
    slug = models.SlugField(unique=True)
    excerpt = models.TextField()
    excerpt_markdown = models.TextField( editable=False, blank=True )
    summary = models.TextField()
    summary_markdown = models.TextField(editable=False, blank=True)
    #workaround = models.TextField()
    #workaround_markdown = models.TextField(editable=False, blank=True)
    date_added = models.DateTimeField()
    poster = models.ForeignKey(User)

class Category ( models.Model ):
    name = models.CharField( max_length=100 )
    parent_id = models.IntegerField()
+1  A: 

You don't need a specific third table. Django's ManyToManyField - which is what you want here for the relationship between Entires and Categories - automatically takes care of the joining table, unless you specifically want to control it (eg if you need to store extra data on the join).

As you recognise in your comment, django-mptt is the best bet for storing the hierarchical relationship of categories. Once you've done that, there's nothing particularly difficult in the fact that you have a ManyToMany relationship between entries and categories - you would just need to show a separate tree for every category the entry is in:

{% for category in my_entry.categories.all %}
    {{ category.show_tree }}
{% endfor %}

where show_tree is the method that draws the tree for that category - which you'd need to define, along the lines of the answer to your last question.

Edit

The main issue with the models as you have them now is that there are no relationships. At the very least, you'll need a ManyToMany relationship between Bug and Category, and a ForeignKey relationship from Category to itself (ie from one category to itself). Adding MPTT onto the Category model on top of that - via level, tree_id, left and right fields which will be added automatically by django-mptt - will make it easier to get all parents or children in one go.

class Bug( models.Model ):
    name = models.CharField( max_length=100 )
    slug = models.SlugField(unique=True)
    excerpt = models.TextField()
    excerpt_markdown = models.TextField( editable=False, blank=True )
    summary = models.TextField()
    summary_markdown = models.TextField(editable=False, blank=True)
    #workaround = models.TextField()
    #workaround_markdown = models.TextField(editable=False, blank=True)
    date_added = models.DateTimeField()
    poster = models.ForeignKey(User)
    categories = models.ManyToManyField('Category')

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

mptt.register(Category)

Now, given a single Bug element, you can get its associated categories with mybug.categories.all(), and for each category you can get its ancestors with category.get_ancestors(). See the mptt docs for more things you can do, especially the provided template tags for showing trees.

Daniel Roseman
do you know if `django-mptt` is compatible with Django 1.2 or 1.1?
meder
Certainly with 1.1. I've not used it with 1.2, but can't see any reason it would be incompatible.
Daniel Roseman
I updated the op with my schema, do you mind just pointing out how you would personally adjust these models? I think I'm overthinking this, and I probably don't need my `parent_id` column.
meder