On Sunday, December 11, 2011 1:53:11 PM UTC-5, tsvim wrote:
>
> db.define_table('table_settings',
>                 Field('name','string'),
>                 Field('default_value','string'))
>
> db.define_table('data',
>                 
> Field('parent_table',db.table_settings,writable=False,readable=False),
>                 Field('datetime','datetime',default=request.now),
>                 Field('title','string'),
>                 Field('value','string'))
>
> db.data.value.default = db.table_settings['parent_table'].default_value
> db.table_settings.default_value.requires = IS_IN_SET(['a','b'])
>

It looks like you want the default value of db.data.value to be dependent 
on the particular parent_table selected. Obviously, this cannot be 
determined until after the parent_table has been selected. What do you want 
the user experience to be? Are you creating a form where the user will 
select a parent_table (i.e., one of the values stored in table_settings)? 
Upon selection, do you want the 'value' field to then be dynamically 
populated with the default_value from table_settings? If so, you'll need to 
set up an Ajax call on the client side. Another simpler alternative is 
using a computed field:

    Field('value', compute=lambda r: 
db.table_settings[r.parent_table].default_value)

Note, by default, computed fields are not displayed in SQLFORMs. If you 
want the field to be displayed, you'll have to explicitly list the fields 
you want shown in the form. If the user leaves that field blank, the value 
will be computed as above -- otherwise, the user's submitted value will be 
inserted. Note, in this case, the user will not see any default value 
pre-populated in the field -- it will simply be blank and filled in on the 
server side after submission if no value has been entered.

See http://web2py.com/book/default/chapter/06#Computed-Fields.

Anthony

Reply via email to