我正在编写一个小型 sqlalchemy shim 来从 MySQL 数据库中导出数据,并进行一些轻量级的数据转换——主要是更改字段名称。我当前的脚本工作正常,但需要我基本上描述我的模型两次——一次在类声明中,一次作为要迭代的字段名称列表。
我试图弄清楚如何使用自省来识别作为列访问器的行对象的属性。以下工作几乎完美:
for attr, value in self.__class__.__dict__.iteritems():
if isinstance(value, sqlalchemy.orm.attributes.InstrumentedAttribute):
self.__class__._columns.append(attr)
除了我的对多关系访问器也是 sqlalchemy.orm.attributes.InstrumentedAttribute 的实例,我需要跳过这些。在我检查类字典时,有什么方法可以区分两者吗?
我在 sqlalchemy introspection 上找到的大多数文档都涉及查看 metadata.table,但由于我正在重命名列,因此该数据并非可轻松映射。