how to display the value of a reference field, instead of the id in sqlform.grid

Showing 1-7 of 7 messages
how to display the value of a reference field, instead of the id in sqlform.grid dave 10/7/12 12:26 AM

grid
= SQLFORM.grid(query, args=[request.args(0)],
                                    fields
=[db.auth_user.test0,
                                    db
.auth_user.test1,
                                    db
.auth_user.test2,
I have defined this in my controller, the value of test1 is a reference to another table, how would I make web2py show the referenced value instead of displaying the id, something equivalent to Field('field2', 'reference employees', requires=IS_IN_DB(db, 'employees.id', '%(name)s')) when defining the table
Re: how to display the value of a reference field, instead of the id in sqlform.grid Anthony 10/7/12 7:37 AM
How is your "test1" field defined?
Re: how to display the value of a reference field, instead of the id in sqlform.grid dave 10/7/12 11:44 AM
auth.settings.extra_fields['auth_user']= [
 
#Field('name', requires=IS_NOT_EMPTY()),
 
.
 
.
 
.
 
Field('test1', 'reference ranking', requires=IS_IN_DB(db, 'ranking.id', '%(name)s')),
 
.
 
.
 
.
Re: how to display the value of a reference field, instead of the id in sqlform.grid Anthony 10/7/12 6:25 PM
If you do:

Field('test1', 'reference ranking')

and then make sure the "format" attribute of the "ranking" table is set to '%(name)s', then the "test1" field will automatically get the IS_IN_DB validator you have explicitly defined below, and it will automatically get a "represent" attribute displaying the "name" value from the "ranking" table. However, if you explicitly define your own "requires" attribute, then you don't get the automatic "represent" attribute, and you have to explicitly define that as well (I think we should probably change this, though).

Anthony
Re: how to display the value of a reference field, instead of the id in sqlform.grid dave 10/7/12 7:17 PM
Thank you very much, I did not see this in the documentation and yes it would be nice if it is consistent with your expectation
Re: how to display the value of a reference field, instead of the id in sqlform.grid Nguyen Minh Tuan 5/3/13 9:22 AM
Hi Anthony,
 
This is seem not work in case self reference.
Ex :
Field('boss', 'reference auth_user')
 
How can I solve in this situation?
 
Thanks,
Re: how to display the value of a reference field, instead of the id in sqlform.grid Anthony 5/3/13 10:41 AM
What doesn't work exactly? With self references, you have to define your own validator and represent attribute because the table doesn't yet exist at the time the Field is created.

Anthony