假设我们创建了这样的模型:
def model_factory(*args, **kwargs):
attrs = {}
attrs['Meta'] = type('Meta', (object,), {
'verbose_name': 'My Dynamic model',
'db_table': 'dynamic_model_table'
})
# Ex. add an arbitrary fields
attrs['username'] = models.CharField(
max_length = 128,
db_column = 'username'
)
attrs['email'] = models.CharField(
max_length = 128,
db_column = 'email'
)
return type('DynamicModel', (models.Model,), attrs)
如何在数据库中指定列顺序?即CREATE语句必须是
CREATE TABLE "dynamic_model_table" (
"username" varchar(128) NOT NULL,
"email" varchar(128) NOT NULL
)
然而,使用代码示例,列顺序将是随机的。