我是使用python3的新手 我有桌子钱包:
ID name score1 score2
1 name1 10 11
2 name2 1 2
3 name3 5 6
我如何查询结果:
total_score = score1 + score 2
ID name total_score
1 name1 21
2 name2 3
3 name3 11
然后我尝试将hybrid_method添加到电子钱包模型中:
@hybrid_method
def total_score(self, fields):
return sum(getattr(self, field) for field in fields)
@total_score.expression
def total_score(cls, fields):
return sum(getattr(cls, field) for field in fields)
AND:
wallets = Wallet.total_score(['socre1', 'score2']).label('total_score')
但是没有任何东西给钱包变量。 我正在使用代码:SqlAlchemy (Postgres + Flask ) : How to sum multiple columns?