Thank you Calvin;
I had to do the following changes, changing 'items.sort(...)' to
'sorted(items,...)', but the drop down displays the item_typess in natural
sort order:
import re
def natural_key(string_):
return [int(s) if s.isdigit() else s for s in re.split(r'(\d+)', string_)]
db.define_table('item_type',
Field('name'),
Field('description'),
format='%(name)s'
)
db.define_table('item',
Field('name'),
Field('description'),
Field('item_type'),
format='%(name)s'
)
db.item.item_type.requires = IS_EMPTY_OR(IS_IN_DB(db, db.item_type,
'%(name)s', zero=None))
if db(db.item_type).isempty():
db.item_type.insert(name='Type1')
db.item_type.insert(name='Type2')
db.item_type.insert(name='Type3')
db.item_type.insert(name='Type10')
db.item_type.insert(name='Type20')
db.item_type.insert(name='Type30')
db.item_type.insert(name='Type99')
items = db(db.item_type.id > 0).select()
sorted_items= sorted(items, key=lambda x: natural_key(x.name))
db.item.item_type.widget = lambda f, v: SELECT(['']+[OPTION(i.name,
_value=i.id) for i in sorted_items], _name=f.name, _id="%s_%s" %
(f._tablename, f.name), _value=v, value=v)
Also note the addition of [''] and IS_EMPTY_OR(...); this will also handle
the default of not-set/None item_types!
Thanks again;
-rppowell
--
---
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/groups/opt_out.