You haven't defined a "represent" attribute for the field, so when you set 
the field to read-only, it just displays the actual value, which is the 
integer ID. This doesn't happen when the field is writable because the 
IS_IN_DB validator generates a select widget that shows the "name" values. 
Try:

Field(..., represent=lambda id, r: db.AllocationVersion(id).name)

Anthony

On Monday, May 4, 2015 at 11:56:36 AM UTC-4, Lieven Van Acker wrote:
>
> I need to build a custom form to perform a database operation on a set of 
> records. I'm using SQLFORM.factory to build the custom form.
> Depending on the request vars, the form will change defaults and form 
> element behaviour (the element can be writable or not, depending on how the 
> request vars are given)
>
> I have a form field that is a reference to a record in a db table in the 
> defined model.
>
>
> writable = some_condition()
>
> form_fields = []
> form_fields.append(Field('AllocationScenario',
>                          writable=writable,
>                          default=allocation_version.id,
>                          requires=IS_IN_DB(db, 
> db.AllocationVersion.id,'%(Name)s'))
>                    )
>
> ...
>
> form =  SQLFORM.factory(*form_fields)
>
>
> If the element representing the form var needs to be editable (when 
> some_condition evaluates True), it is shown as expected as a select 
> element, with options populated from the references db table.
>
> On the other hand, if I some_condition evaluates False, the element is 
> shown as the provided default value (i.e. an integer id value), instead of 
> the corresponding label of the selected option.
>
> Wouldn't it be more appropriate to set the displayed value to the 
> corresponding label of the select option?
>
> What is the best way to work around this issue?
>
>
>

-- 
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