As an example of a multiple select field definition:

db.define_table('fruit',Field('name'))
db.define_table('fruit_basket',Field('contents'))
db.fruit_basket.contents.requires=IS_IN_DB(db,'fruit.id','%
(name)s',multiple=True)

db.fruit.insert('apple')
db.fruit.insert('orange')
db.fruit.insert('banana')

When I subsequently edit one fruit_basket record with crud, I get a
multiple select drop-down of all the available fruit. So far so good.

When this record is subsequently displayed, the fruit_basket contents
field gets output as a list of ids like |1|2|3|  .I actually want a
list of fruit to appear.

This is how I managed it using the represents property of a field:

db.define_table('fruit_basket',Field('contents',
                                'text',
                                represent=lambda fruit_ids: 
get_fruit_list(fruit_ids)))

def get_fruit_list(fruit_ids):
    stripped_list =  fruit_ids.lstrip('|').rstrip('|')
    if stripped_list=='':
        return "No fruit"
    return ', '.join([row.name for row in
db(db.fruit.id.belongs(stripped_list.split('|'))).select()])

This works, but it seems wrong that I'm manually parsing the text
field. Anyone aware of a more elegant way to do this? (like calling
into a pre-existing web2py function to get back the ids as a list?)

IS_IN_DB(...,multiple=True) is a validator used by SQLFORM / crud.
Another way of stating my question is if I directly access a multiple
value field, how can I best access and manipulate this field the same
way that the validator code would?

Also just out of curiosity, anyone know why a multiple select field
starts and ends with a | so values are like |1|2|3| instead of 1|2|
3 ?

Thanks

-- 
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/web2py?hl=en.

Reply via email to