views:

15

answers:

2

Hello. I have this code:

class Article(models.Model):
    # usefull staff
    category = models.ForeignKey("Category")  
class Category(models.Model):
     parent = models.ForegnKey('self')

I want to display categories selector as a <select> field with sorted category tree in it. It should look like

top level category1
  lower level1
  lower level2
    even lower level
    even lower level 2
  lower level3
top level category2
  lower level500

Which is the best way to do this? I hoped to use inheritance from ForeignKey class, but it's sophisticated. Maybe the whole question is "How to inherit from multiply inherited classes?"

A: 

Use django-mptt to represent the category tree in the database. Then you can sort by the 'left' value of each category.

Daniel Roseman
A: 

I did it by inheriting ForeignKey class and overriding method

def formfield():
   return MyChoiceField(choices = get_category_tree())

and MyChoiceField is a child of ChoiceField, where I've corrected validation.

Vasiliy Stavenko