我是新手用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
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)
以及:
wallets = Wallet.total_score(['socre1', 'score2']).label('total_score')
但没有什么能让钱包变化无常。 我在:SqlAlchemy (Postgres + Flask ) : How to sum multiple columns?中使用代码
目前没有回答
相关问题 更多 >
编程相关推荐