master/detail & CRUD id assignment

54 views
Skip to first unread message

Lucas

unread,
Sep 29, 2025, 3:48:51 PMSep 29
to py4web
hello one and all,

back in web2py days of ancient days, you could have something like this in the detail form under a master row:

frm = Form(db.lectures, record=None, deletable=False)
frm.vars['course_id'] = some_id_integer_of_the_master_under_the_function_in_the_controller

and web2py would have filled in that course_id field value when the detail form was submitted.  I've tried the above with py4web and no luck.  

I also tried embedding it under the hidden parameter under Form and the <input type="hidden" name="course_id" value="that_master_integer_id"> does show up under the form in the browser and when submitted does return course_id under request.forms or request.POST, but py4web does not update the field in the database.

so what is the best way to inject a master id into a detail form?  thank you in advance, Lucas

Lucas

unread,
Oct 2, 2025, 8:26:14 AMOct 2
to py4web
did I ask the wrong question?  I've tried multiple "creative" alternatives and I'm still not able to get py4web to update the course_id field in the actual database.  seems to me that the hidden parameter of Form is still the best way to do this.

Lucas

unread,
Oct 4, 2025, 4:18:07 PMOct 4
to py4web
sooooo, I got this code to work under the controller function:

db.lecture.course_id.writable = True
#frm = Form(db.lecture, record=lidi, deletable=False, hidden={'course_id':sR.id}, _id="edit")
frm = Form(db.lecture, record=lidi, deletable=False, _id="edit")
frm.vars['course_id'] = sR.id
flash.set(frm.__dict__)
#frm[0]['_style'] = 'display: none;'
if frm.accepted:
    #frm.vars['course_id'] = sR.id
    flash.set(frm.vars)
    redirect(URL(f'lectures/{ridi}',  vars={'lidi':frm.vars.get('id')}))
elif frm.errors:
    flash.set(f'errors: {frm.errors}')

where ride and lidi are integers passed to the controller function and sR is a single row referring to the courses table.

this is quite messy.  I have to set """db.lecture.course_id.writable = True""" or else it won't write to the db.  but that makes course_id visible under the form and allows the user to change it.  bad because that should be a protected or hidden field.  you'll also see that I tried to hide that input and associated divs with commented variations on the """frm[0]['_style'] = 'display: none;'""" line but I could not access the elements or children or find of the form itself.  I guess accessing the form elements is out also.

so, darn it, it still makes sense that the hidden parameter of Form should update the db field instead of all of these darned messy work arounds.  OR, am I just crazy?  frustrated for a graceful solution,,, lucas

Christian Varas

unread,
Oct 4, 2025, 9:47:42 PM (14 days ago) Oct 4
to Lucas, py4web
Hi, I'm not sure if I understand what you need/mean but if you are trying to make a field in a form with a default value and also be hidden, do this: 

db.lecture.course_id.default = some value
db.lecture.course_id.readable = False
db lecture.course_id writable = False

form = Form(db.table)


Gettings.
--
You received this message because you are subscribed to the Google Groups "py4web" group.
To unsubscribe from this group and stop receiving emails from it, send an email to py4web+un...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/py4web/d38c39d4-592a-409f-93f6-82bd38e8cfben%40googlegroups.com.

Lucas

unread,
Oct 4, 2025, 11:14:29 PM (14 days ago) Oct 4
to py4web
yup, you understood perfectly,  thank you, and I'm thinking to myself, daaaa, ya big dummy, so simple and I didn't think of that.  maybe I'm too much in a rut.  thank you again.  Lucas
Reply all
Reply to author
Forward
0 new messages