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:

  1. how (using current solution) have invoice.amount_due_rands column_proprty return invoice.total_rands if there no payments invoice.

  2. 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

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -