Django 中的 Expando 模型

发布于 2024-08-20 03:12:43 字数 117 浏览 4 评论 0原文

是否可以在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蓝海 2024-08-27 03:12:43

这是有可能的,但这将是一个史诗般的大杂烩。 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:

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文