Consider this
db.define_table(*'home'*,
Field('home_name', 'string', requires=[IS_NOT_EMPTY(), IS_NOT_IN_DB(db,
'home.home_name')]),
format='%(home_name)s')
db.define_table('other',Field(*'home'*, 'reference home', label='Home ID'))
format='%(home_name)s' tells web2py how home is to be represented in
reference. Otherwise it uses the id.
This:
db.other.home.requires=IS_IN_DB(db, 'home.id')
is optional. It gives your the dropdown.
db.other.home.requires=[IS_IN_DB(db, 'home.id')]
still validates but no dropdown but you get an INPUT for the reference ID.
The presence of format='%(home_name)s also implies
db.other.home.represent = lambda id, row: '%(home_name)s' % db.home(id)
which you can customize and it is used in list.
if your home_names are unique and you do not want the dropdown you can
override the INPUT with an autocomplete widget
db.other.home.widget=SQLFORM.widgets.autocomplete(request,db.home.home_name,db.home.id)
home stores an ID which you see represented as
On Saturday, 29 December 2012 15:02:09 UTC-6, Ilya wrote:
>
>
> *Issue:*
>
> 1. I have a field in a DB table like this:
>
> Field(*'home'*, 'reference home', requires=IS_IN_DB(db, 'home.id'),
> label='Home ID')
>
> 2. 'reference home' is a separate DB table like this:
>
> # A table to store home names
> db.define_table(*'home'*,
> Field('home_name', 'string', requires=[IS_NOT_EMPTY(),
> IS_NOT_IN_DB(db, 'home.home_name')]))
>
> 3. Now, when I have this in the controller:
>
> form = SQLFORM(db.user_home, fields=[*'home'*]).process()
>
> It shows the *'home'* field as a dropdown in the view
>
> *Questions:*
> *
> *
> a) How do I show the field (via SQLFORM) as a text field instead of as a
> dropdown?
>
> b) Maybe more generally, how do I choose how the DB field will be
> represented (dropdown, checkbox, field, etc.)? (since it seems to by
> default represent this field as a dropdown for me)
>
--