annoying behaviour with computed fields

100 views
Skip to first unread message

simon

unread,
Apr 18, 2012, 12:51:23 PM4/18/12
to web...@googlegroups.com
I have a person table with date of birth and a compute field for age. I want my form to show date of birth (writable) and age (read only). When the form is accepted I reread the table and create a new SQLFORM with the new age.

However I cannot get this to work.
  • On a SQLFORM the age does not show up by default even if readable=True.
  • If I include age in the "fields" parameter and writable=True then it shows up but is editable.
  • If I set writable=False then it does not recalculate the computed field.
Wouldn't it be better for computed fields to show up if readable=True;  and to be updated even if writable=False?




Richard Vézina

unread,
Apr 19, 2012, 1:52:42 PM4/19/12
to web...@googlegroups.com
I think you can't have your computed field to display in the form... I mean it makes no sens anyway, since the user should not be allow to enter a value in this field since it a calculated field.

I am not sure about the behavior of readable and writable field in case of computed field in a form and why it should prevent the field to compute in case you set to writable=False. For sure combination of both field parameter (readable and writable) on Update, Read and Select should works as describe in the book :

readable if a field is readable, it will be visible in readonly forms. If a field is neither readable nor writable, it will not be displayed in create and update forms.

writable if a field is writable, it can be edited in autogenerated create and update forms.


Anyway, when I use computed field, in the create form, I just leave the default web2py behavior, I mean, the field is not there. By default, it should be there in the read version of the form and not there in the update version of the form. And it should be there in the select.

If this is not good for you, you may try different combination of readable and writable, true or false, at the same time to see if you can set the proper behavior you are searching for. Or you may just give up if you can't get it and explain your users...

Hope it help.

Richard

pbreit

unread,
Apr 19, 2012, 2:30:13 PM4/19/12
to web...@googlegroups.com
Pretty much what Richard said. If you don't want the computed functionality, just write the logic in the controller.

simon

unread,
Apr 19, 2012, 3:54:54 PM4/19/12
to web...@googlegroups.com
I can certainly workaround it by putting the age calculation in the onvalidation function; and then when it is accepted I reread the record and recreate the form.

However it is inconsistent with the book that a readable compute field is not shown on the form; and is not the most obvious behaviour that a non-writable compute field is not updated.

Richard Vézina

unread,
Apr 19, 2012, 3:59:39 PM4/19/12
to web...@googlegroups.com
What do you mean by reread and recreate?? You create a record, you update (modify) a record and you read a record.

What behavior is not correct, I think also, we would need to see your code, maybe you have some error in your code that bring you to think that it is not working as expect.

Richard

simon

unread,
Apr 19, 2012, 4:09:15 PM4/19/12
to web...@googlegroups.com
See below. The age field is read only and the form is already created when you do the accept. So to get the updated age I recreate the form with the new data. It does work though I think it would have been neater to have the age as a compute field in the model.

def onvalidation(form):
    """ calculates fields before update """
    form.vars.age=calc.age(form.vars.date_of_birth)

def edit():
    customer=db.customer[request.args(0)]
    
    form = SQLFORM(db.customer, customer)
    if form.process(onvalidation=onvalidation).accepted:
        # recreate form with calculated fields
        customer=db.customer[request.args(0)]
        form = SQLFORM(db.customer, customer)

Anthony

unread,
Apr 19, 2012, 4:20:35 PM4/19/12
to web...@googlegroups.com
I agree.
 

Richard Vézina

unread,
Apr 19, 2012, 4:26:23 PM4/19/12
to web...@googlegroups.com
I just read this again... Maybe you want some kind of ajax triggred computed field??

I mean you want to show to your user that base on the birth date they add entered that the age is X before the even submit the form.

So, to me it is a client side processing base on the field input.

Richard

On Wed, Apr 18, 2012 at 12:51 PM, simon <sim...@gmail.com> wrote:

Richard Vézina

unread,
Apr 19, 2012, 4:12:14 PM4/19/12
to web...@googlegroups.com
Model please!

I would see you lambda computed field.

Richard
Reply all
Reply to author
Forward
0 new messages