SQLAlchemy 查询元组值

SQLAlchemy query tuple values

我正在尝试将 SQLAlchemy 查询的输出显示到神社模板中。 我的查询如下:

query = db.session.query(models.Server, models.Scan).outerjoin(models.Server.scans).all()

其输出为:

[(<Server u'Testing'>, <Scan u'testscan'>), (<Server u'fasd'>, <Scan u'testscan'>), (<Server u'fdaafas'>, None)]

服务器 table 包含三列:name, description and ip
扫描 table 包含另外三列:type, scan_id and timestamp.

我想做的是访问服务器描述和相应的 scan_type。 我试过使查询成为字典,但仍然无法访问相关值。任何帮助都会很棒!

当你在 SQLAlchemy 中进行连接时,至少在你将模型指定为你的 select 的情况下,每条记录都会作为连接模型对象的元组返回。您拥有的是服务器和扫描对象的元组列表。 query[i] 生成一个元组,query[i][0] 生成一个服务器对象,query[i][1] 生成一个扫描对象。您所要做的就是 query[i][0].description 获取服务器描述和 query[i][1].type 获取扫描类型。