views:

193

answers:

1

Is it possible to implement 'expando' model in Django, much like Google App Engine has? I found a django app named django-expando on github but it's still in early phase.

+2  A: 

It's possible, but it would be a kludge of epic proportions. GAE uses a different database design known as a column-based database, and the Django ORM is designed to link with relational databases. Since technically everything in GAE is stored in one really big table with no schema (that's why you don't have to syncdb for GAE applications), adding arbitrary fields is easy. With relational databases, where each table stores exactly one kind of data (generally) and has a fixed schema, arbitrary fields aren't so easy.

One possible way you could implement this is to create a new model or table for expando properties that stores a table name, object ID, and a TextField for pickled data, and then have all expando models inherit from a subclass that overrides the __setattr__ and __getattr__ methods that will automatically create a new row in this table. However, there are a few major problems with this:

  • First off, it's a cheap hack and is contrary to the principles of relational databases.
  • Second, it is not possible to query these expando fields without even more hacks, and even so it would be ludicrously slow.

My recommendation is to find a way to design your database structure so that you don't need expando models.

LeafStorm
thanks :)I am just exploring the expando model. I think the app engine patch or appengine-helper is perfect for me.
koko