我一直在寻找一种方法来定义数据库表并通过 Django API 更改它们。
例如,我想编写一些直接操作表 DDL 的代码,并允许我以编程方式按需定义表或将列添加到表中(无需运行同步数据库)。我意识到可能会想到 django-south 和 django-evolution,但我并不认为这些工具是旨在集成到应用程序中并由最终用户使用的工具......相反,这些工具是用于升级您的数据库表。我正在寻找可以执行以下操作的东西:
class MyModel(models.Model): # wouldn't run syncdb.. instead do something like below
a = models.CharField()
b = models.CharField()
model = MyModel()
model.create() # this runs the create table (instead of a syncdb)
model.add_column(c = models.CharField()) # this would set a column to be added
model.alter() # and this would apply the alter statement
model.del_column('a') # this would set column 'a' for removal
model.alter() # and this would apply the removal
这只是一个关于这种 API 如何工作的玩具示例,但重点是我很想知道是否有一种方法可以像这样以编程方式创建和更改表。这对于诸如内容管理系统之类的东西可能很有用,其中人们可能想要动态创建一个新表。另一个例子是存储任意宽度数据集的站点,需要通过接口或数据导入动态生成表。有人知道动态创建和更改这样的表的任何好方法吗?
(当然,我知道可以对数据库执行直接 SQL 语句,但该解决方案缺乏将数据库视为对象的能力)
只是好奇人们是否对此有任何建议或方法......