Search code examples
pythondjangodjango-modelssingle-table-inheritance

Single Table Inheritance in Django


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).


Solution

  • There are currently two forms 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.

    You can also reference the official docs on model inheritance.