python - Sqlalchemy column_property math -
i'm working invoice , payment models, trying determine how money due paid invoice based on payments invoice.
i have come following, making use of sqlalchemy's column_proprty:
# total amount invoice invoice.total_rands = column_property( select([func.sum(invoiceproduct.sub_total_rands)]).where(invoiceproduct.invoice_id==invoice.id).correlate(invoice) ) # total amount needs paid. invoice.amount_due_rands = column_property( invoice.total_rands - select([func.sum(payment.amount_rands)]).where(payment.invoice_id==invoice.id).correlate(invoice) )
this issue above code if there no payments invoice, invoice's 'amount_due_rands' none , not equal invoice.total_rands.
so guess have 2 questions:
how (using current solution) have invoice.amount_due_rands column_proprty return invoice.total_rands if there no payments invoice.
how else approach problem?
invoice.amount_due_rands = column_property( invoice.total_rands - select([func.coalesce(func.sum(payment.amount_rands), 0)]).where(payment.invoice_id==invoice.id).correlate(invoice) )
Comments
Post a Comment