开发者

Expando Model in Django

Is it possible to implement 'expando' model in Django, much like Googl开发者_JAVA百科e App Engine has? I found a django app named django-expando on github but it's still in early phase.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜