Precise behaviour of "compute" on record update

27 views
Skip to first unread message

cjrh

unread,
Nov 18, 2010, 2:46:32 AM11/18/10
to web2py-users
Regarding the "compute" attribute of a Field object, the book says
this:

"compute is an optional function. If a record is inserted and there is
no value for a field that has a compute attribute, the value is
calculated by passing the record (as a dict) to the compute function."

My current testing, on 1.89.3, shows that compute will run not only
when a record is inserted, but indeed every time any other field in
that same record is updated. I have a field that is a uuid which is
only supposed to be calculated once, on insertion. Also, the
compute function is passed the current record field values as a dict,
but that excludes any value the Field with compute() might currently
have.

In my opinion, the text in the book is misleading. Thoughts?

mdipierro

unread,
Nov 18, 2010, 7:05:22 AM11/18/10
to web2py-users
true. the text should be fixed.

cjrh

unread,
Nov 18, 2010, 8:05:20 AM11/18/10
to web2py-users
"I have a field that is a uuid which is
only supposed to be calculated once, on insertion."

Best way to do this currently? I would use "default" if within the
context of a controller, but these are records I create manually in
admin. There is a field that I want populated with a newly-calculated
uuid, but only on insertion of a new record, and it must not be
recalculated again.

mdipierro

unread,
Nov 18, 2010, 8:46:02 AM11/18/10
to web2py-users
>>> db=DAL()
>>> db.define_table('xxx',Field('yyy',writable=False,readable=False))
>>> db.xxx.yyy.default=lambda: 'hello world'
>>> db.xxx.insert()
1
>>> print db.xxx(1).yyy
hello world

default can be a lambda.

cjrh

unread,
Nov 18, 2010, 2:53:04 PM11/18/10
to web2py-users
On Nov 18, 3:46 pm, mdipierro <mdipie...@cs.depaul.edu> wrote:
> >>> db=DAL()
> >>> db.define_table('xxx',Field('yyy',writable=False,readable=False))
> >>> db.xxx.yyy.default=lambda: 'hello world'
> >>> db.xxx.insert()
> 1
> >>> print db.xxx(1).yyy
>
> hello world
>
> default can be a lambda.

I did not know that. I'm going to update the book.

cjrh

unread,
Nov 19, 2010, 2:05:36 AM11/19/10
to web2py-users
On Nov 18, 3:46 pm, mdipierro <mdipie...@cs.depaul.edu> wrote:
> >>> db=DAL()
> >>> db.define_table('xxx',Field('yyy',writable=False,readable=False))
> >>> db.xxx.yyy.default=lambda: 'hello world'
> >>> db.xxx.insert()
> 1
> >>> print db.xxx(1).yyy
>
> hello world
>
> default can be a lambda.

If I do this inside db.py, then the database administration system
inside admin shows this under my field when inserting a new record in
the admin UI:

<function <lambda> at 0x0B4C14B0>

And it also shows this in my field after insertion, when viewing the
contents of the table. This is on 1.89.3. Either my behaviour is
different to yours, or there is something fundamental that I
misunderstand about how the admin database administration UI works.


cjrh

unread,
Nov 19, 2010, 4:43:52 AM11/19/10
to web2py-users
I solved my problem using a custom validator.
Reply all
Reply to author
Forward
0 new messages