I have written code for managing conditional insert/update/delete to multiple tables from single form. (I had posted my code-attempt in earlier thread, but seems it was lost in the heap of other threads. Hence posting it again here. I agree, the code is in very raw form & may not be ‘pythonic’. There are code repeatitions. But at least I have something to go ahead & build a refined structure. I present my code as under. (Comments are given to explain the flow)
I would highly welcome any ideas/suggestions for improvements. ########## Code starts here ########## MODELS: db.define_table('mdlmst', Field('mdlmstid','id'), Field('mdlmstcd'), Field('mdlmstnm'), migrate=False, format='%(mdlmstnm)s' ) db.define_table('wrmst', Field('wrmstid','id'), Field('wrmstcd'), Field('wrmstnm'), migrate=False, format='%(wrmstnm)s' ) db.define_table('extwrmst', Field('extwrmstid','id'), Field('extwrmstcd'), Field('extwrmstnm'), migrate=False, format='%(extwrmstnm)s' ) # from the FORM, data will be populated in the following two tables db.define_table('mdlwr', Field('mdlwrid','id'), Field('mdlmstid',db.mdlmst), Field('wrmstid',db.wrmst), migrate=False ) db.define_table('mdlextwr', Field('mdlextwrid','id'), Field('mdlmstid',db.mdlmst), Field('extwrmstid',db.extwrmst), migrate=False ) CONTROLLERS: ### ‘modelwar’ controller will render the records from ‘mdlmst’ table def modelwar(): models = db(db.mdlmst.mdlmstid>0).select(orderby=db.mdlmst.mdlmstnm) return dict(models=models) ### after clicking a particular record, ‘war_edit’ controller will manage the tables – ‘mdlwr’ & ‘mdlextwr’ def war_edit(): mdl_id = request.args(0) # variable identifying the ‘mdlmstid’ (which record to be modified) mdl_nm = request.args(1) # variable for getting ‘mdlmstnm’ warset = db(db.mdlwr.mdlmstid==mdl_id) # fetch a set extwarset = db(db.mdlextwr.mdlmstid==mdl_id) warlist = db(db.mdlwr.mdlmstid==mdl_id).select() # get a ROW object extwarlist = db(db.mdlextwr.mdlmstid==mdl_id).select() form_war=FORM(TABLE(TR("Basic Warranty", SELECT(_type="select",_name="baswar",*[OPTION(x.wrmstnm,_value=x.wrmstid) for x in db().select(db.wrmst.ALL)]), TR("Extended Warranty", SELECT(_type="select",_name="extwar",*[OPTION(x.extwrmstnm,_value=x.extwrms tid)for x in db().select(db.extwrmst.ALL)]), TR("", INPUT(_type='submit',_value='Save')), )))) # pre-populate the fields in‘form_war’ if len(warlist)>0: form_war.vars.baswar = warlist[0].wrmstid if len(extwarlist)>0: form_war.vars.extwar = extwarlist[0].extwrmstid # after successful form submission, manage the table 'mdlwr' if form_war.accepts(request.vars, session): if len(warlist)>0: # if there was any record in the list fetched from database & sent to FORM, if form_war.vars.baswar=='': # delete if value returned from FORM field is blank warset.delete() else: warset.update(wrmstid=form_war.vars.baswar) # else update, else: db.mdlwr.insert(mdlmstid=mdl_id, wrmstid=form_war.vars.baswar) # else insert # Similarly, manage the table 'mdlextwr' if len(extwarlist)>0: if form_war.vars.extwar=='': extwarset.delete() else: extwarset.update(extwrmstid=form_war.vars.extwar) else: db.mdlextwr.insert(mdlmstid=mdl_id, extwrmstid=form_war.vars.extwar) response.flash = 'Warranty definition saved' return dict(form_war=form_war,mdlnm=mdl_nm) VIEW for 'mdlmst' table {{response.files.append(URL(r=request,c='static',f='jquery.dataTables.min.j s'))}} {{response.files.append(URL(r=request,c='static',f='demo_table.css'))}} {{extend 'layout.html'}} <script>jQuery(document).ready(function() { jQuery('.smarttable').dataTable();});</script> <h1>Modelwise Warranty Master</h1> <table class="smarttable"> <thead> <tr> <th>Model ID</th><th>Model Code</th><th>Model Name</ th> </tr></thead> <tbody>{{for model in models:}} <tr> <td>{{=model.mdlmstid}}</td> <td>{{=model.mdlmstcd}}</td> <td>{{=model.mdlmstnm}}</td> <td>{{=A('edit warranty',_href=URL('war_edit',args=[model.mdlmstid,model.mdlmstnm]))}} </tr> {{pass}} </tbody> </table> #### Code ends here #### A generic class is needed to be written for managing actions (insert/ update/delete) based on the results fetched from database & return values from FORM. Hopefully, the same might be useful to many other web2py programmers also. I look forward to your ideas for enhancement. Thanks, Vineet