gpt4 book ai didi

python - 在渲染模板时如何引用 sqlalchemy 中的自定义字段?

转载 作者:行者123 更新时间:2023-12-01 05:54:02 26 4
gpt4 key购买 nike

我正在尝试打印 func.count() 调用的值,但是一旦我进入 mako 模板,我就很难弄清楚它是如何命名的。我尝试在调试器行中 pprint 和 dict 变量,但它们作为对象返回,所以我不确定从这里去哪里。

这是我正在执行的 View 的代码:


TodayList = DBSession.query(
func.count(ExtendedCDR.uniqueid),ExtendedCDR,ExtensionMap)
.filter(or_(
ExtensionMap.exten == ExtendedCDR.extension,
ExtensionMap.prev_exten == ExtendedCDR.extension))
.filter(ExtendedCDR.start > func.curdate())
.group_by(ExtendedCDR.extension)
.order_by(func.count(ExtendedCDR.uniqueid).desc())
.all()

当此代码运行时,sql 查询的调试返回:


2012-11-09 19:16:10,392 INFO [sqlalchemy.engine.base.Engine][Dummy-3] SELECT count(cdr_extended.uniqueid) AS count_1, cdr_extended.uniqueid AS cdr_extended_uniqueid, cdr_extended.start AS cdr_extended_start, cdr_extended.end AS cdr_extended_end, cdr_extended.clid AS cdr_extended_clid, cdr_extended.src AS cdr_extended_src, cdr_extended.dst AS cdr_extended_dst, cdr_extended.dcontext AS cdr_extended_dcontext, cdr_extended.channel AS cdr_extended_channel, cdr_extended.dstchannel AS cdr_extended_dstchannel, cdr_extended.lastapp AS cdr_extended_lastapp, cdr_extended.lastdata AS cdr_extended_lastdata, cdr_extended.duration AS cdr_extended_duration, cdr_extended.billsec AS cdr_extended_billsec, cdr_extended.disposition AS cdr_extended_disposition, cdr_extended.amaflags AS cdr_extended_amaflags, cdr_extended.accountcode AS cdr_extended_accountcode, cdr_extended.userfield AS cdr_extended_userfield, cdr_extended.extension AS cdr_extended_extension, concat(concat(ext_map.<code>FName</code>, %s), ext_map.<code>LName</code>) AS anon_1, ext_map.exten AS ext_map_exten, ext_map.<code>FName</code> AS <code>ext_map_FName</code>, ext_map.<code>LName</code> AS <code>ext_map_LName</code>, ext_map.login_name AS ext_map_login_name, ext_map.<code>isManager</code> AS <code>ext_map_isManager</code>, ext_map.prev_exten AS ext_map_prev_exten
FROM cdr_extended, ext_map
WHERE (ext_map.exten = cdr_extended.extension OR ext_map.prev_exten = cdr_extended.extension) AND cdr_extended.start > curdate() GROUP BY cdr_extended.extension ORDER BY count(cdr_extended.uniqueid) DESC

这是我的 mako 模板代码:


% for topentry in TopToday:

${topentry.ExtensionMap.AssociateName}
${topentry.count_1}

% endfor

但是,Mako 会抛出 ${topentry.count_1} 不存在的异常,尽管人们会假设它会给出上面的 DEBUG 输出。

有谁知道我如何找到 count_1 根据 python/Mako 命名的内容?

最佳答案

您应该能够使用 .label() 命名查询的返回值。例如,DBSession.query(func.count(ExtendedCDR.unique_id).label('count'), ...)

关于python - 在渲染模板时如何引用 sqlalchemy 中的自定义字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13314587/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com