If you benefit from web2py hope you feel encouraged to pay it forward by contributing back to society in whatever form you choose!

Given a database f_bottles, with an arbitrary fields and some custom fields representing guests bringing a bottle of wine to a Tasting Party. a one bottle and many (1-2) tasters relation. (Rest assured, there is also a one to many relation for the tasting.)

We first create the form.

form=SQLFORM.factory(
    Field('name_1', type='string', notnull=True, unique=True,
        label=T('Taster 1'), 
    ),
    Field('name_2', type='string', notnull=False, 
        label=T('Taster 2'),
     ),
     db.t_bottles)

Now we customize the form, by wrapping the <fieldset/> around the current table, while adding the <legend/>. Note that the form-table can be reached by using form[0]

form[0]=FIELDSET(TAG.legend("Tasters"),form[0],_id="register")

A more complex case, adding multiple fieldsets

fs0=form[0][:2]     # the first two name rows
fs1=form[0][2:-1]   # the wine data rows (all but the last)
fs2=form[0][-1]     # submit row (last) 

form[0]=TABLE(
    FIELDSET(TAG.legend("Tasters"),TABLE(fs0),_id="register0"),
    FIELDSET(TAG.legend("Wine"),TABLE(fs1),_id="register1"),
    TABLE(fs2))

# handle the submit    
if form.accepts(request.vars,session):
    bottle_id = db.t_bottles.insert(**db.t_bottles._filter_fields(form.vars))
    id = db.t_tasters.insert(f_name=form.vars.name_1, f_bottle=bottle_id)
    id = db.t_tasters.insert(f_name=form.vars.name_2, f_bottle=bottle_id)
    response.flash='Wine and guest data are now registered'

return dict(form=form)

Related slices

Comments (1)


Hosting graciously provided by:
Python Anywhere