[web2py] Re: compute fields on update

2011-07-14 Thread pbreit
That definitely looks like it should work according to the book.

What do you get when you do it exactly like the book?

 db.define_table('item',
Field 
http://web2py.com/book/default/docstring/Field('unit_price','double'),
Field 
http://web2py.com/book/default/docstring/Field('quantity','integer'),
Field http://web2py.com/book/default/docstring/Field('total_price',
compute=lambda r: r['unit_price']*r['quantity']))
 r = db.item.insert(unit_price=1.99, quantity=5)
 print r.total_price


What version of Web2py? Do you get different behavior if you pull the 
compute out of the table def?

db.item.total_price.compute = lambda r: r['unit_price']*r['quantity']

How about: 
db.item.total_price.compute = lambda r: float(r['unit_price']) * 
float(r['quantity'])


[web2py] Re: compute fields on update

2011-07-14 Thread niknok
You're checking it from the shell. Do a db.commit() before checking
for total_price.

On Jul 15, 3:37 am, guruyaya guruy...@gmail.com wrote:
 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


[web2py] Re: compute fields on update

2011-07-14 Thread guruyaya
 db.commit()
 db(db.item.id == 1).select()[0].total_price
'9.95'
 db(db.item.id == 1).select()[0]
Row {'total_price': '9.95', 'update_record': function lambda at
0x17f1758, 'unit_price': 3.0, 'id': 1, 'delete_record': function
lambda at 0x17f17d0, 'quantity': 5}


On Jul 15, 12:54 am, niknok nikolai...@gmail.com wrote:
 You're checking it from the shell. Do a db.commit() before checking
 for total_price.

 On Jul 15, 3:37 am, guruyaya guruy...@gmail.com wrote:







  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


[web2py] Re: compute fields on update

2011-07-14 Thread guruyaya
As for what you said, pbreit - this is acctually a copy paste of the
book. The version installed is 1.97.1

As for your your 2nd suggestion
 db.item.total_price.compute = lambda r: float(r['unit_price']) * 
 float(r['quantity'])
 r = db.item.insert(unit_price=1.99, quantity=3)
 r.total_price
'5.97'
 db.commit()
 r.id
2
 db(db.item.id0).select();
gluon.dal.Rows object at 0x17f2450
 db(db.item.id0).select()[1]
Row {'total_price': '5.97', 'update_record': function lambda at
0x17f1c80, 'unit_price': 1.99, 'id': 2, 'delete_record': function
lambda at 0x17f1cf8, 'quantity': 3}

 db(db.item.id==2).select()[0];
Row {'total_price': '5.97', 'update_record': function lambda at
0x17f1b18, 'unit_price': 1.99, 'id': 2, 'delete_record': function
lambda at 0x17f1d70, 'quantity': 3}
 db(db.item.id==2).update(quantity=12);
1
 db.commit()
 db(db.item.id0).select()[1]
Row {'total_price': '5.97', 'update_record': function lambda at
0x17f1f50,
Row {'total_price': '5.97', 'update_record': function lambda at
0x17f1f50, 'unit_price': 1.99, 'id': 2, 'delete_record': function
lambda at 0x17f7050, 'quantity': 12}

So nothing yet...
On Jul 14, 11:18 pm, pbreit pbreitenb...@gmail.com wrote:
 That definitely looks like it should work according to the book.

 What do you get when you do it exactly like the book?

  db.define_table('item',

         Field 
 http://web2py.com/book/default/docstring/Field('unit_price','double'),
         Field 
 http://web2py.com/book/default/docstring/Field('quantity','integer'),
         Field http://web2py.com/book/default/docstring/Field('total_price',
             compute=lambda r: r['unit_price']*r['quantity']))

  r = db.item.insert(unit_price=1.99, quantity=5)
  print r.total_price

 What version of Web2py? Do you get different behavior if you pull the
 compute out of the table def?

 db.item.total_price.compute = lambda r: r['unit_price']*r['quantity']

 How about:
 db.item.total_price.compute = lambda r: float(r['unit_price']) *
 float(r['quantity'])


[web2py] Re: compute fields on update

2011-07-14 Thread niknok
I just tested this on v1.97.1 , and confirm that from the shell the
record doesn't appear to be updated even after a db.commit(). However,
using appadmin to check the database, the records were actually are
updated.

But if do it like this:
for i in db(db.item.id0).select(): i.quantity, i.unit_price,
i.total_price

instead of like:
items =db(db.item.id0).select()
for i in items: i.quantity, i.unit_price, i.total_price

You will see the record values are immediately updated.

So, I think this is a bug unless I'm very much mistaken.



On Jul 15, 12:17 pm, guruyaya guruy...@gmail.com wrote:
  db.commit()
  db(db.item.id == 1).select()[0].total_price
 '9.95'
  db(db.item.id == 1).select()[0]

 Row {'total_price': '9.95', 'update_record': function lambda at
 0x17f1758, 'unit_price': 3.0, 'id': 1, 'delete_record': function
 lambda at 0x17f17d0, 'quantity': 5}

 On Jul 15, 12:54 am, niknok nikolai...@gmail.com wrote:







  You're checking it from the shell. Do a db.commit() before checking
  for total_price.

  On Jul 15, 3:37 am, guruyaya guruy...@gmail.com wrote:

   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