10 February 2009

Function calculated fields with stored values

Function fields which store the calculations results are important to make function fields fast enough not only with test data but also with bigger databases. In v5 the recalculation of stored functions can be triggered by the changes (write, create) in other objects and that makes them really useful.
There's an example in account.invoice where the fields amount_untaxed, amount_tax and amount_total are computed by the same function, _amount_all. An shortened example:
'amount_total': fields.function(_amount_all, method=True, digits=(16,2), string='Total',
store={
'account.invoice.line': (_get_invoice_line, None, 20),
},),
This means that if an account.invoice.line changes, _get_invoice_line is invoked with the line's id. _get_invoice_line has to return an id of the main object, account.invoice. _amount_all will receive that id and compute the value to be stored in the field.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.