How about doing your own widget:
import re
def natural_key(string_):
return [int(s) if s.isdigit() else s for s in re.split(r'(\d+)', string_)]
items = db(db.item_type.id > 0).select()
sorted_items= items.sort(key=natural_key(items.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)
On Saturday, 11 May 2013 04:11:41 UTC, rppowell wrote:
>
> I have pre-populated a table of types by name:
> 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='Type11'...)
> ...
>
> I want to use the table as selection in another table, (using
> requires=IS_IN_DB(db, db.item_type, '%(name)s'):
> db.define_table('item',
> Field('item_type', db.item_type),
> )
> db.item.item_type.requires = IS_IN_DB(db, db.item_type, '%(name)s')
>
> When I use a form to add a new record, the items are displayed out of
> order in the dropdown:
> Type1
> Type10
> Type11
> ...
> Type2
>
> How do I specify using natural sorting, for example, using the following
> python:
>
> import re
> def natural_key(string_):
> return [int(s) if s.isdigit() else s for s in re.split(r'(\d+)',
> string_)]
>
> items = [
> 'Item99', 'Item1', 'Item10', 'Item20', 'Item2', 'Item3', 'Item30'
> ]
> items.sort(key=natural_key)
> print ' '.join(items)
>
>
>
> How do I do this with the db in web2py? Do a query, sort the list, and
> use it as a IS_IN_SET()?
>
> -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.