Newbie - SQLFORM ordering of reference fields

35 views
Skip to first unread message

M Bailey

unread,
Apr 24, 2014, 8:31:40 AM4/24/14
to web...@googlegroups.com
I'm trying to migrate a django project to web2py. I have defined a search form with <select><option> fields populated from the database and using code similar to below, but I can't see a way of changing the ordering of the SQLFORM fields:

db.define_table('pet_type',
    Field('description', 'string', required=True, notnull=True, length=75),
    format = '%(description)s')

db.define_table('colour_type',
    Field('description', 'string', required=True, notnull=True, length=75),
    format = '%(description)s')


db.define_table('search_form',
    Field('pet_type_ref', 'reference pet_type', label='Pet'),
    Field('colour_type_ref', 'reference colour_type', label='Pet colour'))

In index controller:
form = SQLFORM(db.search_form)
return dict(form=form)

In the view:
{{=form}}


In the admin section, I add some pets into the pet_type table but not in alphabetical order, e.g. Dog, Budgie, Cat

All works brilliantly in the view with <select> controls being used for the reference fields. 

However I can't see a way of changing the ordering of the pets for the select. Currently it defaults to pet_type.description ASC but how would I change it to the pet_type.id so that the records are displayed in the order they are inserted?

Thanks for any advice
Mark

Massimo Di Pierro

unread,
Apr 24, 2014, 9:05:45 AM4/24/14
to web...@googlegroups.com
db.search_form.pet_type_ref.requires = IS_IN_DB(db,'pet_type',orderby=db.pet_type.description)

OR this may work too:

db.search_form.pet_type_ref.requires.orderby = db.pet_type.description 

M Bailey

unread,
Apr 24, 2014, 9:25:48 AM4/24/14
to web...@googlegroups.com


On Thursday, 24 April 2014 14:05:45 UTC+1, Massimo Di Pierro wrote:
db.search_form.pet_type_ref.requires = IS_IN_DB(db,'pet_type',orderby=db.pet_type.description)

OR this may work too:

db.search_form.pet_type_ref.requires.orderby = db.pet_type.description 


Massimo thank you, that second option you suggested is ideal

Thanks for a great framework
Mark
Reply all
Reply to author
Forward
0 new messages