1

假设我们创建一个这样的模型:

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
)

但是,使用代码示例,列顺序将是随机的。

4

0 回答 0