Any query you do will not replace original amount
column. But you can load another column using 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 Transaction
objects, but rather tuple(Transaction, Decimal)
But if you want this property be part of your object, then:
Since your case when ...
function is completely independent from the condition in WHERE
, I would suggest that you change your code in following way:
1) add a property to you object, which does the case when ...
check as following:
@property
def special_amount(self):
return -self.amount if self.transfer_account_id == 1 else self.amount
You can completely wrap this special handling of the amount providing a setter property as well:
@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) fix your query to only have a filter clause with or_
clause (it looks like your query does not work at all):
q = session.query(Transaction).filter(
or_(Transaction.account_id==1,
Transaction.transfer_account_id==1)
)
# then get your results with the proper amount sign:
for t in q.all():
print q.id, q.special_amount