Any query you make will not replace the original column amount. But you can load another column using the following query:
q = session.query(Transaction,
case([(Transaction.transfer_account_id==1, -1*Transaction.amount)], else_=Transaction.amount).label('special_amount')
)
q = q.filter(or_(Transaction.account_id==1, Transaction.transfer_account_id==1))
This will not return only objects Transaction, but rathertuple(Transaction, Decimal)
, , :
case when ... WHERE, :
1) , case when ... :
@property
def special_amount(self):
return -self.amount if self.transfer_account_id == 1 else self.amount
, setter:
@special_amount.setter
def special_amount(self, value):
if self.transfer_account_id is None:
raise Exception('Cannot decide on special handling, because transfer_account_id is not set')
self.amount = -value if self.transfer_account_id == 1 else value
2) or_ (, ):
q = session.query(Transaction).filter(
or_(Transaction.account_id==1,
Transaction.transfer_account_id==1)
)
for t in q.all():
print q.id, q.special_amount