Django 中的 Expando 模型
是否可以在 Django 中实现“expando”模型,就像 Google App Engine 那样?我在 github 上发现了一个名为 django-expando 的 django 应用程序,但它仍处于早期阶段。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是有可能的,但这将是一个史诗般的大杂烩。 GAE 使用不同的数据库设计,称为基于列的数据库,而 Django ORM 旨在与关系数据库链接。由于从技术上讲,GAE 中的所有内容都存储在一个非常大的表中,没有架构(这就是为什么您不必为 GAE 应用程序同步数据库),因此添加任意字段很容易。对于关系数据库,每个表(通常)只存储一种数据并具有固定的模式,任意字段并不那么容易。
实现此目的的一种可能的方法是为 Expando 属性创建一个新模型或表,用于存储表名称、对象 ID 和用于腌制数据的 TextField,然后让所有 Expando 模型继承自重写 __setattr__ 和 __getattr__ 方法的子类,这些方法将自动在此表中创建新行。然而,这样做有几个主要问题:
我的建议是找到一种设计数据库结构的方法,这样您就不需要扩展模型。
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:My recommendation is to find a way to design your database structure so that you don't need expando models.