Hello guys,

I am looking for a DRY way to do this while keeping the file code
maintainable. I appreciate your replies. Thank you.

###
I have already defined my tables in the model file, and now I would
like to have a form created for it.

So I have:
#model
db.define_table('mytable',
  Field('title', 'string', length=255, required=True),
  Field('description', 'text', required=True),
  ...many more user-editable fields here...
  Field('created_by', 'reference auth_users', default=auth.user_id),
  Field('created_on', 'datetime', default=request.now),
  Field('modified_by', 'reference auth_users', update=auth.user_id),
  Field('modified_on', 'datetime', update=request.now),
)

#controller
form = SQLFORM(db.mytable)
return dict(form=form)

###
The form comes out fine, except I would like to have the following
behavior:
- Only allow "title" and "description" (along with many other editable
fields) to be input or editable by the user
- *Either not have the last 4 fields appear at all on the form,
whether hidden from view or not (but the field values are still
inserted into the table by the default values assigned in Model)
- *Or have the last 4 fields as hidden fields on the form (and the
field values are inserted to the table respectively)

I am thinking along the lines of...
- Modify the Model file for the 4 fields to have hidden=True (I
reference web2py book, no "hidden" attribute here:
http://web2py.com/book/default/chapter/06#Record-Representation)
- Use
>> hidden=hidden_fields
in Controller file, e.g.
>> hidden_fields = ('created_on','created_by',...etc...),
where the fields are automatically hidden from view, with the default
values already populated.

However, this is also not possible, since
>> hidden_fields = dict('created_on'=request.now, 'created_by'=....)
means I will have to create each hidden field value again (repeat of
what was already created in Model, i.e. it's not DRY and changes made
to one file may not reflect in another).
In addition, the hidden fields are created on top of the SQLFORM and
doesn't take away the fields with the same names, which means
duplicated fields

Am I missing something here with SQLFORM?
Would the above be possible in some way?
Or must I manually create / list each element in the form in some way?
(i.e. not DRY)

Thank you!

Reply via email to