Skip to main content
 首页 » 编程设计

python之Sqlalchemy:从 sqlalchemy.schema.MetaData 获取 python 类

2025年05月04日9luoye11

我正在尝试预填充关联对象列表。因此,对于特定的映射类,我列出了所有表:

例如,ExampleClass 是我映射的 python 类。

name = ExampleClass.__name__ 
tables = [x for x in ExampleClass.metadata.tables.keys() if x != name ] 

所以,我得到了表名,但我怎样才能得到与该表关联的类?

这可能吗?

我正在使用声明方式来映射表和类。

提前致谢。

请您参考如下方法:

我发现通过这种方式传递在列表表中找到的表名参数

def __find_class(self, table): 
        for x in mapperlib._mapper_registry.items(): 
            if x[0].mapped_table.name == table: 
                return x[0].class_