This is run on a web2py shell:
>>> db.define_table('item',
... Field('unit_price','double'),
... Field('quantity','integer'),
... Field('total_price',
... compute=lambda r: r['unit_price']*r['quantity']))
.
.
.
>>> r = db.item.insert(unit_price=1.99, quantity=5)
>>> r.total_price
'9.95'
>>> db(db.item.id == 1).select()[0]
<Row {'total_price': '9.95', 'update_record': <function <lambda> at
0x17f10c8>, 'unit_price': 1.99, 'id': 1, 'delete_record': <function
<lambda> at 0x17f1230>, 'quantity': 5}>
Till now, all is good.
>>> db(db.item.id == 1).update(unit_price = 3)
1
>>> db(db.item.id == 1).select()[0].total_price
'9.95'
>>> db(db.item.id == 1).select()[0].unit_price
3.0
The web2py book said that
""" When a new record is modified, including both insertions and
*updates*, if a value for the field is not provided, web2py tries to
compute from the other field values using the compute function """
How come? shouldn't the compute field be recalculated?
Now