使用 'type' 动态定义 CQLEngine 模型

Define CQLEngine model dynamically using 'type'

我正在使用 Datastax Cassandra python 驱动程序的对象映射器在 运行 时间定义 cassandra table 列(要求就像那些)。 Table 并且列名和列类型在 运行 时解析。

我正在尝试在 运行 时使用 'type' 定义一个 cassandra cqlenengine 模型来定义一个 class。

看起来 python 驱动程序中定义的模型 class 已将元 class 添加到模型

@six.add_metaclass(模型元类)
class模型(基础模型):
...

有没有一种方法可以使用类型来定义模型? 我在定义模型时看到以下错误 class

from cassandra.cqlengine.models import Model
from cassandra.cqlengine import columns as Columns

attributes_dict = {
    'test_id': Columns.Text(primary_key=True)
    'test_col1': Columns.Text()
}

RunTimeModel = type ('NewModelName', tuple(Model), attributes_dict)


Error:
RunTimeModel = type ('NewModelName', tuple(Model), attributes_dict)
TypeError: 'ModelMetaClass' object is not iterable

我会远离其余部分,但为了回答有关错误的问题,我认为您在尝试从非序列参数构造元组时遇到了一个简单的语法错误。相反,您可以使用元组文字表示法:

RunTimeModel = type ('NewModelName', (Model,), attributes_dict)