Why would you want web2py to auto-compute a field when you have asked
the user for its value? unless you want the computation BEFORE you
prompt the user for a value. But in this case it cannot depend on
other fields because they have not been insrted yet.

On May 12, 9:30 am, Iceberg <iceb...@21cn.com> wrote:
> So if I understand it correctly, the Field(..., compute=...) is only
> suitable for defining a field which is not supposed to be showed in
> SQLFORM(), neither supposed to be filled by human.
>
> For example, this case can use Field(..., compute=...).
>
>     db.define_table('circles',
>         Field('radius', 'double'),
>         Field('area', 'double',
>             compute = lambda record: record['radius']**2*3.14),
>         )
>
> And this case should not. (Why? Try.)
>
>     db.define_table('visitor_booking',
>         Field('adult', 'integer'),
>         Field('kid', 'integer',
>             compute = lambda r: min(r['kid'], r['adult']) ),
>         # each kid must be accompanied by one dedicated adult
>         )
>
> PS: Of course the latter case should have better approach. Here I am
> just demostrating when and when not to use Field(..., compute=...)
>
> Regards,
> iceberg
>
> On May12, 2:22am, Iceberg <iceb...@21cn.com> wrote:
>
> > Thanks for the tip, but there seems some more subtle difference
> > between form.accepts(..., onvalidation=callback) VS Field('bar',
> > compute=lambda r:r['foo']).
>
> > The latter will cause that field no longer showts up in SQLFORM,
> > because its default self.fields excludes all compute field. Is there
> > any reason for that?
>
> > Right now I still have to stick to my form.accepts(...,
> > onvalidation=callback) approach. :-/
>
> > Sincerely,
> > Iceberg
>
> > On May12, 1:30am, mdipierro <mdipie...@cs.depaul.edu> wrote:
>
> > > Yes but
>
> > >   Field('bar', compute=lambda r:r['foo'])
>
> > > not
>
> > >   Field('bar', compute=lambda r:r.foo)
>
> > > On May 11, 11:28 am, Iceberg <iceb...@21cn.com> wrote:
>
> > > > I must miss the "compute" feature. Now I am picking it up. Would you
> > > > please confirm whether my understanding is correct?
>
> > > > The code:
> > > >         Field('bar', compute=lambda r:r.foo)
> > > > is an easier equivalent for:
> > > >         def callback(form): form.vars.bar = form.vars.foo
> > > >         form.accepts(..., onvalidation=callback)
> > > > and it only works before inserting or updating a record into db.
>
> > > > On the contrary, rows.setvirtualfields(...), only works when
> > > > retrieving records from db. (Detail 
> > > > inhttp://groups.google.com/group/web2py/browse_frm/thread/d93eee8cc2495c8c
> > > >  )
>
> > > > Is it right?
>
> > > > Regards,
> > > > iceberg
>
> > > > On May11, 10:23pm, mdipierro <mdipie...@cs.depaul.edu> wrote:
>
> > > > > I think you look for something like
>
> > > > > db.table.field_2.compute=lambda r: request.vars.field_1
>
> > > > > On May 11, 4:01 am, AsmanCom <d.as...@web.de> wrote:
>
> > > > > > That would be a great solution, but i´ve to do this in model 
> > > > > > because i
> > > > > > am using the awesome JQGrid plugin (app.ebansoftware.net/
> > > > > > editable_jqgrid/).
> > > > > > The dilemma is:
> > > > > > - I need to do this in the model
> > > > > > - I can`t get the vars from table.field.default (self-evident)
> > > > > > - I need a working IS_IN_DB(db,'table.id','table.name') validator 
> > > > > > and
> > > > > > label (for the JQGrid plugin)
> > > > > > - I can´t add an aditional validator (because IS_IN_DB +label don´t
> > > > > > show up right, when combined with other validators)
> > > > > > - The form must be visible to the user, so i can´t go with
> > > > > > table.field.compute
>
> > > > > > So I think to implement this with Field default function is the 
> > > > > > right
> > > > > > approach?
> > > > > > But I need to catch the vars, compute someth. and Insert.
>
> > > > > > Any ideas?
>
> > > > > > On 10 Mai, 19:47, Iceberg <iceb...@21cn.com> wrote:
>
> > > > > > > Model:
> > > > > > > db.define_table('table_1',
> > > > > > >                        Field('field_1'),
> > > > > > >                        Field('field_2')
> > > > > > >                        )
>
> > > > > > > Controller:
> > > > > > > def index():
> > > > > > >   def magic(form):
> > > > > > >     form.vars.field_2 = form.vars.field_1
> > > > > > >   form = SQLFORM(db.table_1)
> > > > > > >   if form.accepts(request.vars, onvalidation=magic):
> > > > > > >     pass # ok
> > > > > > >   return {'form': form}
>
> > > > > > > I did not test it. But it shows you the key of using 
> > > > > > > onvalidation()
> > > > > > > trick.
>
> > > > > > > Regards,
> > > > > > > Iceberg
>
> > > > > > > On May11, 12:08am, AsmanCom <d.as...@web.de> wrote:
>
> > > > > > > > Hi,
>
> > > > > > > > i want to do a simple task:
>
> > > > > > > > def function_1():
> > > > > > > >     var_field_1=request.vars.field_1
> > > > > > > >     if var_field_1:
> > > > > > > >         return var_field_1
>
> > > > > > > > db.define_table('table_1',
> > > > > > > >                        Field('field_1','string' ),
> > > > > > > >                        Field('field_2', default=function_1)
> > > > > > > >                        )
>
> > > > > > > > db.table_1.field_1.default='Autogenerated'
> > > > > > > > db.table_1.field_2.default='Autogenerated'
>
> > > > > > > > I want to catch default vars from field 1 and insert them in 
> > > > > > > > field_2
> > > > > > > > instead.
> > > > > > > > Is this possible?
>
> > > > > > > > ###becomes interesting when combined, with IS_IN_DB and the
> > > > > > > > get_or_create function###

Reply via email to