Anthony --

One more tiny but not insignificant detail...

I found I had to add "requires=[]" to the custom.widget:

    [form.custom.widget[f.name].update(_readonly=True, requires=[]) for f in 
readonly_fields]

Otherwise, the field keeps the 'requires' of the original Field and the 
check fails.  The above solution is working very well!

-- Joe



On Friday, March 31, 2017 at 8:49:51 AM UTC-7, Anthony wrote:
>
> We should probably make what you are doing a built-in option (maybe even 
> the default), but for now, you can try something like this:
>
> def sqlform2(*args, **kwargs):
>     table = kwargs.get('table', args[0])
>     fields = kwargs.get('fields', [f for f in table])
>     readonly_fields = [f for f in fields if not f.writable]
>     [setattr(f, 'writable', True) for f in readonly_fields]
>     form = SQLFORM(*args, **kwargs)
>     [form.custom.widget[f.name].update(_readonly=True) for f in 
> readonly_fields]
>     [setattr(f, 'writable', False) for f in readonly_fields]
>     return form
>
> def myform():
>     form = sqlform2(db.mytable, request.args(0)).process()
>     return dict(form=form)
>
> The above sets the readonly fields to writable before creating the form, 
> and then sets the _readonly HTML attribute of the widgets after creating 
> the form (you could also set _disabled=True if desired). It then resets all 
> the readonly fields so they are not writable before returning, so when the 
> form is processed, those fields will not be included in the database write.
>
> Anthony
>
>

-- 
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
--- 
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to