Model inheritance, Python subclass or one-to-one relationship?

110 views
Skip to first unread message

Dado

unread,
Oct 30, 2005, 6:43:39 PM10/30/05
to Django users
I am a little confused about how to implement model inheritance in
Django. The docs talk about inheriting model classes using Python
inheritance mechanism, which creates duplicate fields in tables.
Alternatively, the docs suggest using a "one-to-one" relationship field
as an "is-a" relationship. Which one is the best way of doing model
inheritance in Django? Is there anything like SQLObject v0.7
inheritance in Django?

Moreover, is it possible to have abstract model classes?

Thanx,

Dado

Robert Wittams

unread,
Oct 30, 2005, 6:47:02 PM10/30/05
to django...@googlegroups.com

Dado

unread,
Oct 30, 2005, 7:17:26 PM10/30/05
to Django users
Hi Robert,

thanx for your prompt reply.

In your original post you ask for 'inheritance' features that I also
really need. However, were your requests implemented? If so, it is not
documented! The way SQLObject v7 does this is to add an child_name
column to the superclass table and follow the inheritance hierarchy
downward <http://sqlobject.org/Inheritance.html>. I don't know how
performant this implementation is, but it feels very pythonic in the
way model classes behave.

For example, I would like to have a Node superclass which implements
node-to-node relationships to build a graph-like structure of my
website. I would then like to create subclasses of Node for different
content types (Pages, Images, Contacts, etc.)... with all of them
sharing the Node attributes (e.g., ID, title, description, created,
modified, etc) and behavior (getChildren, getParents, getSiblings,
getRelated). How would I do this with Django as it stands now? And how
far away in the future are the inheritance features that you requested
going to be implemented?

Thanx,

Dado

Robert Wittams

unread,
Oct 30, 2005, 8:04:33 PM10/30/05
to django...@googlegroups.com
Dado wrote:
> Hi Robert,
>
> thanx for your prompt reply.
>
> In your original post you ask for 'inheritance' features that I also
> really need. However, were your requests implemented? If so, it is not
> documented! The way SQLObject v7 does this is to add an child_name
> column to the superclass table and follow the inheritance hierarchy
> downward <http://sqlobject.org/Inheritance.html>. I don't know how
> performant this implementation is, but it feels very pythonic in the
> way model classes behave.

This was more my plan of what I am going to do in a branch after the
new-admin branch merges.

There is a discussion about the various ways of mapping subtyping
relationships to database in Martin Fowlers Patterns Of Application
Architecture. The relevant patterns are Single Table Inheritance,
Concrete Table Inheritance, Class Table Inheritance. I'm leaning towards
the latter, as it means adding a subclass does not change your original
database tables.

I might support Single Table Inheritance (what sqlobject seems to use
from your description) as well, as a special subclass of meta.Model
(used as the base of a hierarchy), basically as a performance hack, if
it seems to be required.

Concrete Table Inheritance is very difficult to get right (identity
problems), so I'm not sure if it is worth doing.

Dado

unread,
Oct 30, 2005, 9:56:03 PM10/30/05
to Django users
SQLObject uses "Class Table Inheritance".

I would say that giving the developer a choice of STI and CTI (perhaps
via an option in META) for different use cases it's the best solution.
If not possible, I would go with CTI!!!

I am a Django newb but I would really love to implement what I have in
mind using Django instead of TurboGears/Subway. Any idea when the CTI
branch will be merged?!? And whether it will be there in 1.0?!?

Having CTI (perhaps in addition to STI) would really make Django better
than Rails.

Dado

Reply all
Reply to author
Forward
0 new messages