Search code examples
web2py

Calculate a field in Web2py


I need a calculated field that is the product of 2 other fields in the same table. The fields is Total, which is equal to Price * Quantity in an Stock table.

Would someone provide an example, please?


Solution

  • You probably want a computed field:

    Field('total', compute=lambda r: r.price * r.quantity)
    

    Depending on your needs, you might also consider a virtual field.