Your format is a function therefore db.t_birthplace._format % row
should be db.t_birthplace._format(row) On Wednesday, 17 September 2014 22:00:37 UTC-5, Fei Zhao wrote: > > I would like to generate a dropdown list dynamically according to the > records of a table, which refers to other tables. But it seems that lambda > doesn't work with OPTION helper. > > For instance, I defined four tables, t_country, t_state, t_city, and > t_birthplace. Table t_birthplace refers to those three tables, and I want > the birthplace to be shown as "country, state, city". > The following is my code. > > In model: > > db.define_table('t_country', > Field('f_name')) > > db.define_table('t_state', > Field('f_name')) > > db.define_table('t_city', > Field('f_name')) > > db.define_table('t_birthplace', > Field('f_country','reference t_country'), > Field('f_state','reference t_state'), > Field('f_city','reference t_city'), > format=lambda r: '%s %s %s' % (db(db.t_country.id == r. > f_country).select().first().f_name, > db(db.t_state.id == r. > f_state).select().first().f_name, > db(db.t_city.id == r.f_city > ).select().first().f_name)) > > > In controller: > > def index(): > db.t_country.insert(f_name='US') > db.t_state.insert(f_name='CA') > db.t_city.insert(f_name='San Mateo') > db.t_birthplace.insert(f_country=1,f_state=1,f_city=1) > > rows_birthplace = db(db.t_birthplace).select() > # db.t_birthplace._format = '%(f_country)s' > opts_birthplace = [OPTION(db.t_birthplace._format % row, _value=row.id > ) for row in rows_birthplace] > form = FORM(SELECT(_name='sel_birthplace', *opts_birthplace)) > return dict(form=form) > > > In view: > > {{extend 'layout.html'}} > {{=form}} > > > I got error while running the above code: > > opts_birthplace = [OPTION(db.t_birthplace._format % row, _value=row.id) > for row in rows_birthplace] > TypeError: unsupported operand type(s) for %: 'function' and 'Row' > > > But if I redefine the format, uncomment "db.t_birthplace._format = > '%(f_country)s'" in controller, then the error is gone, the dropbox is > generated, but the representation of the option is not what I wanted, it > just shows 1, the id of f_country which refers to table t_country. > > How can I get the dropdown list that shows the name nicely like "US, CA, > San Mateo"? > Your suggestion is highly appreciated! > -- 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 web2py+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.