After some thought, I'm really liking this design for virtual
fields... what if lazy/virtual fields were declared directly in
db.define_table()? Like so:
db.define_table('item',
Field('unit_price','double'),
Field('quantity','integer'),
VirtualField('total_price',
lambda self:
self.item.unit_price*self.item.quantity,
lazy=True))
It's so simple.
I still kinda feel like we might find better names than lazy/virtual
though. So here's a design I like even more:
db.define_table('item',
Field('unit_price','double'),
Field('quantity','integer'),
Method('total_price',
lambda self:
self.item.unit_price*self.item.quantity,
precompute=False))
`precompute' means "not lazy" and would default to false.
On Aug 25, 6:14 am, Massimo Di Pierro <[email protected]>
wrote:
> We are moving away from this because of many problems. Try this
> instead. It is still experimental but may go into stable soon.
>
> def vfields():
> db.define_table('item',
> Field('unit_price','double'),
> Field('quantity','integer'))
> db(db.item.id>0).delete()
>
> db.item.lazy_total_price=Field.lazy(lambda
> self:self.item.unit_price*self.item.quantity)
>
> db.item.bulk_insert([{'unit_price':12.00, 'quantity': 15},
> {'unit_price':10.00, 'quantity': 99},
> {'unit_price':120.00, 'quantity': 2},])
> res = []
> for r in db(db.item.id>0).select():
> res.append([r.unit_price, r.quantity, r.lazy_total_price()])
> return dict(res=res)
>