Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

IS_IN_DB ability to sort selections?

185 views
Skip to first unread message

Jon

unread,
Dec 27, 2008, 12:41:57 AM12/27/08
to web2py Web Framework
I am using this in my model to achieve a drop-down selection in my
controller forms, hence views:

db.menu_item.restaurant.requires=IS_IN_DB
(db,'restaurant.id','restaurant.name')

However, I don't know how I can sort the results to not be
alphabetical, but rather, a manually assigned sort column I've
provided myself (or, perhaps, simply the order in which they were
added to the database).

Any ideas?

Jon

unread,
Dec 27, 2008, 1:10:22 AM12/27/08
to web2py Web Framework
I'd add one follow-up to this to (still need the first answer)... I'm
curious to know how hard it is to express a many-to-many selection
(e.g. check all related items that apply to this record's item) in an
automatically built form? Or do you have to do that manually...

mdipierro

unread,
Dec 27, 2008, 3:15:12 AM12/27/08
to web2py Web Framework
You have to do that manually for now.

Jon

unread,
Dec 27, 2008, 11:51:20 PM12/27/08
to web2py Web Framework
Sounds good. So even for alphabetical sorting, the right approach is
to just return my data in the right order via the controller and then
have the view iterate through it to build the HTML drop-down manually?
If so, that is fine--just confirming best design.

mdipierro

unread,
Dec 28, 2008, 3:43:55 AM12/28/08
to web2py Web Framework
in trunk now... IS_IN_DB(....orderby=....)

Massimo

Jon

unread,
Dec 28, 2008, 3:41:59 PM12/28/08
to web2py Web Framework
Massimo--

Thanks! This is fantastic. This is one of the main reasons I like this
framework--it is a tightknit community.

I am thinking that the new custom form helper will give me enough
insight to build something to handle my many-to-many form field needs.
My vision from an automated perspective would be that you could do an
"IS_IN_DB" referencing a many-to-many relationship and specify
something like "checkbox" or "multi-select list" to have it built with
the SQLFORM command (and handle the many-to-many join table data...).
But I will see if I can get by with the custom form for now.

Jon

mdipierro

unread,
Dec 28, 2008, 4:06:01 PM12/28/08
to web2py Web Framework
The problem with many2many is not so much an implementation issues as
much as a logical one. There is a reason SQL does not implement APIs
for many2many. Django does what it does by considering a only one case
of many2many, i.e. when there is no info associated to the link.

think of buyer - purchase - product
purchase estabilishes a many2many relation between buyer and product
but it also stores information (quantity of the product, price paid,
date of purchase, etc.)

How should the interface handle the general case? Is there even a
general case?

Implementing a many2many a la Django without information stored in the
links may force developer to make wrong design choices.

Massimo

Ross Peoples

unread,
May 11, 2011, 1:51:23 PM5/11/11
to web...@googlegroups.com
On Sunday, December 28, 2008 3:43:55 AM UTC-5, mdipierro wrote:
in trunk now... IS_IN_DB(....orderby=....) 



I realize that this thread is like 2.5 years old, but this should be in the book. I was looking for this functionality and found it here.
Reply all
Reply to author
Forward
0 new messages