一些警告:
将一个计算列添加到查询中的正确方法是什么,这样它就包含在返回的对象中,而不是作为返回元组中的单独条目?
db_query = session.query(Listing)
db_query = db_query.add_column("1 as listing_test")
print(self.db_query)
## this looks good.
# SELECT listing.id AS listing_id, 1 as listing_test
# FROM listing
# WHERE listing.id = :id_1
self.db_query.all()
## this looks bad.
# [(<flask_sqlalchemy.Lease object at 0x7fca868ff1d0>, 1)]
self.db_query.column_descriptions
[{'aliased': False,
'entity': <class 'flask_sqlalchemy.Lease'>,
'expr': <class 'flask_sqlalchemy.Lease'>,
'name': 'Lease',
'type': <class 'flask_sqlalchemy.Lease'>},
{'aliased': False,
'entity': None,
'expr': '1 as listing_test',
'name': '1 as listing_test',
'type': NullType()}]
理想情况下,listing_test不是当前返回的元组,而只是返回的列表对象的一部分。
答案 0 :(得分:0)
@univerio让我非常接近!
我必须将它设置为列属性,然后一切都很好。
class Listing(db.model):
custom_function = column_property(func.custom_function(id))