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)
On Aug 25, 7:50 am, Martin Weissenboeck <[email protected]> wrote:
> I wanted to learn more about lazy virtual fields and therefore I have
> repeated the example from the book:
>
> def vfields():
> db.define_table('item',
> Field('unit_price','double'),
> Field('quantity','integer'))
>
> db(db.item.id>0).delete()
>
> class MyVirtualFields:
> def lazy_total_price(self):
> return lambda self=self: self.item.unit_price*self.item.quantity
>
> db.item.virtualfields.append (MyVirtualFields())
>
> 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)
>
> The expected output is:
> [[12.0, 15, 180.0], [10.0, 99, 990.0], [120.0, 2, 240.0]]
>
> But I got
> * [[12.0, 15, *240.0]*, [10.0, 99, *240.0*], [120.0, 2, 240.0]]*
> *
> *
> *Three times the same result.
> *
> I have read the book and my program over and over again - but I cannot see
> any error.*
> *
>
> Does somebody have an idea?
> Martin