The SQLFORM options widget ignores the value in the database when
editing the field.  The following code shows the failure:

Model:
db.define_table('product_types', Field('name', length=32,
label='Type'))

product_type_list = [r.name for r in
db().select(db.product_types.name)]
db.define_table(
    'products',
    Field('name', length=256, required=True, notnull=True),
    Field('product_type', 'list:string'),
    format = '%(name)s',
    )

db.products.product_type.requires = IS_IN_SET(product_type_list,
    zero='Choice required')

Controller:
grid = SQLFORM.grid(...)
return dict(grid=grid)

To work around add something like this before the call to grid:
  if len(request.args)>2 and request.args[-3] == 'edit':

        product_type_value =
db.products[request.args[-1]].product_type

        rows = db((db.product_types.is_active==True) &
 
(db.product_types.tenant_link==session.auth.user.tenant_link)
                 ).select(db.product_types.name)

        product_type_list = [r.name for r in rows]

        db.products.product_type.widget = lambda field,value: \
            SELECT(product_type_list, value=product_type_value)

Reply via email to