views:

1207

answers:

5

Is there explicit support for Single Table Inheritance in Django? Last I heard, the feature was still under development and debate.

Are there libraries/hacks I can use in the meantime to capture the basic behavior? I have a hierarchy that mixes different objects. The canonical example of a corporation structure with an Employee class, subclasses for types of employees, and a manager_id (parent_id) would be a good approximation of the problem I am solving.

In my case, I would like to represent the idea that an employee can manage other employees while being managed by a different employee. There are not separate classes for Manager and Worker, which makes this hard to spread across tables. Sub-classes would represent types of employees-programmers, accountants, sales, etc and would be independent of who supervises who (OK, I guess it's no longer a typical corporation in some respect).

+1  A: 

You could have one employee table, an on each of the "sub-tables" you use a ForeginKey to point to the corresponding emloyee.

class Employee(models.Model):
    first_name = models.CharField()
    last_name = models.CharField()

class Programmer(models.Model):
    employee = models.ForeignKey(Employee)
    language = models.CharField()

    def first_name(self):
        return self.employee.first_name
    def last_name(self):
        return self.employee.last_name

Edit: It seems Django supports table inheritence similar to the one above. http://docs.djangoproject.com/en/dev/topics/db/models/#id4

MizardX
better update your example to reflect the django documentation
Andre Bossard
A: 

Are you talking about Model Inheritance? If so, it's there. If not, please provide some more detailed example.

S.Lott
+7  A: 

There are currently two form of inheritance in Django - MTI (model table inheritance) and ABC (abstract base classes).

I wrote a tutorial on what's going on under the hood here: http://thisweekindjango.com/articles/2008/jun/17/abstract-base-classes-vs-model-tab/

I hope this helps.

You can also reference the official docs: http://docs.djangoproject.com/en/dev/topics/db/models/#model-inheritance

montylounge
+6  A: 

I think the OP is asking about Single-Table Inheritance as defined here: http://www.martinfowler.com/eaaCatalog/singleTableInheritance.html. That is, a single database table for a whole hierarchy of entity classes. Django does not support that kind of inheritance.

Björn Lindqvist
A: 

I think you can do something akin to this.

I have to implement a solution for this problem myself, and here was how I solved it:

class Citrus(models.Model)
    how_acidic = models.PositiveIntegerField(max_value=100)
    skin_color = models.CharField()
    type = models.CharField()

class TangeloManager(models.Manager)
    def get_query_set(self):
        return super(TangeloManager, self).get_query_set().filter(type='Tangelo')

class Tangelo(models.Model)
    how_acidic = models.PositiveIntegerField(max_value=100)
    skin_color = models.CharField()
    type = models.CharField()
    objects = TangeloManager()
    class Meta:
        # 'appname' below is going to vary with the name of your app
        db_table = u'appname_citrus'

This may have some locking issues... I'm not really sure how django handles that off the top of my head. Also, I didn't really test the above code, it's strictly for entertainment purposes, to hopefully put you on the right track.

James