我最近更新了我的SQLAlchemy和FormAlchemy的最新版本,一件奇怪的事情发生了。
我确实使用Mako模板来显示来自我的模型的数据。
Python:
class Asset(object):
id = Column(Integer, primary_key=True, nullable=False)
...
@hybrid_property
def underscore_name(self):
return "x_underscore_name_x"
Mako模板:
<li>Item: ${Asset.underscore_name}</li>
在升级之前,呈现文本的网页是:
Item: x_underscore_name_x
升级后显示:
Item: Asset.underscore_name
重要!正在执行该方法,但返回的结果未在网页上呈现。有什么想法吗?
编辑:
负责此行为的库是SQL >=1.1.0。版本1.0.19没有此问题。让我们看看开发人员的反应。
发布于 2018-03-08 08:23:51
由于迈克尔拜尔的礼貌,他评论了报告的问题#4214: hybrid_property名称正在显示,而不是返回的值
Asset.underscore_name的值是一个SQL表达式,因为11.html#hybrid-properties-and-methods-now-propagate-the-docstring-as-well-as-info中描述的更改来自于Asset.underscore_name所理解的返回对象的11.html#hybrid-properties-and-methods-now-propagate-the-docstring-as-well-as-info方法。上面说明的混合语句没有正确编写,因为它没有返回核心SQL表达式对象:
s = Session()
print(s.query(Asset.underscore_name))
sqlalchemy.exc.InvalidRequestError: SQL expression, column, or mapped entity expected - got ''x_underscore_name_x''
如果您正在寻找返回字符串的类绑定属性,只需分配它:
class Asset(...):
underscore_name = "x_underscore_name_x"
或者,如果您希望这个方法在类级别上是可调用的,请使用@classproperty,这里有一个菜谱:https://stackoverflow.com/a/5191224/34549和SQLAlchemy有一个您可以复制的方法:https://github.com/zzzeek/sqlalchemy/blob/master/lib/sqlalchemy/util/langhelpers.py#L1140
这里没有错误,因为这不是@hybrid_property的正确用法
非常感谢迈克尔!
https://stackoverflow.com/questions/49154668
复制相似问题