Issue: https://github.com/web2py/web2py/issues/1493
Note, the bug is not specific to .validate_and_insert or computed fields -- it is an incorrect configuration of the IS_EMPTY_OR validator when setting up default validators for fields created without an explicit "requires" attribute. Anthony On Thursday, October 6, 2016 at 1:45:30 PM UTC-4, Anthony wrote: > > Sorry, I was testing with an older version. This bug was introduced in > April with this commit: > https://github.com/web2py/web2py/commit/bdbc053285b67fd3ee02f2ea862b30ca495f33e2. > > Easy fix -- I'll submit an issue. > > Anthony > > On Thursday, October 6, 2016 at 9:27:36 AM UTC-4, Val K wrote: >> >> Yes, it's a bug: >> *validate_and_insert* calls *_validate_fields* that fills fields with >> default values (all table fields!), so for 'y' it's *None* >> from book: >> " *if a value for the field is not provided*, web2py tries to compute >> from the other field values using the compute function" >> but it's provided and set to* "" (i.e.** empty string!)* by >> *_validate_fields! >> *that is converted to* None* while insert >> just try: >> db.test._validate_fields(dict(x=1)) >> >> On Thursday, October 6, 2016 at 3:41:12 PM UTC+3, Yang wrote: >>> >>> OK, I packaged it. >>> >>> model/test.py >>> db.define_table('test', >>> Field('x', 'float'), >>> Field('y', 'float', compute=lambda r: r['x'] * 2)) >>> >>> controller/default.py >>> def index(): >>> db.test.validate_and_insert(x=1) >>> db.test.insert(x=1) >>> result_validated = db.test(1) >>> result_insert = db.test(2) >>> return locals() >>> >>> views/default/index.html >>> {{=result_validated}} >>> {{=result_insert}} >>> >>> The output: >>> <Row {'x': 1.0, 'id': 1L, 'y': None}> <Row {'x': 1.0, 'id': 2L, 'y': >>> 2.0}> >>> >>> Thanks! >>> >>> >>> -- Resources: - http://web2py.com - http://web2py.com/book (Documentation) - http://github.com/web2py/web2py (Source code) - https://code.google.com/p/web2py/issues/list (Report Issues) --- You received this message because you are subscribed to the Google Groups "web2py-users" group. To unsubscribe from this group and stop receiving emails from it, send an email to web2py+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.