SingleSelectField question

6 views
Skip to first unread message

badfrog

unread,
Sep 7, 2007, 1:01:56 PM9/7/07
to TurboGears
Hello again, and thanks for everyone's kind attention to my previous
dumb questions :)

I'm coming to TG from ASP.NET, which means I love Data Binding, and I
know Python/TG would make binding a dropdown/combo list to a dynamic
set of data really easy, but I'm struggling in figuring it out.

Assuming I have a State model defined with abbreviation and name
fields, how would I build a US states drop down list? Following the
Toolbox SingleSelectField widget example, I've experimentally tried:

states = State.select()
list = widgets.SingleSelectField(options=states)

but apparently either SQLObject isn't a list or select's result isn't
a tuple:

File "c:\python24\lib\site-packages\TurboGears-1.0.3.2-py2.4.egg
\turbogears\wi
dgets\forms.py", line 882, in _extend_options
if (len(opts) > 0) and not isinstance(opts[0], (tuple,list)):
TypeError: len() of unsized object

I know I could iterate over states myself and build a tuple of lists,
but I'm lazy -- is there a way to tell SingleSelectField "for each
object, use these results and use this property as your value and this
property as your text"?

Lukasz Szybalski

unread,
Sep 7, 2007, 2:51:15 PM9/7/07
to turbo...@googlegroups.com
On 9/7/07, badfrog <bad...@w00tzgames.com> wrote:
>
> Hello again, and thanks for everyone's kind attention to my previous
dumb questions :)
>
> I'm coming to TG from ASP.NET, which means I love Data Binding, and I
> know Python/TG would make binding a dropdown/combo list to a dynamic
> set of data really easy, but I'm struggling in figuring it out.
>
> Assuming I have a State model defined with abbreviation and name
> fields, how would I build a US states drop down list? Following the
> Toolbox SingleSelectField widget example, I've experimentally tried:
>
I think you will have to create that list
states = State.select()
statelist=[]
for state in states:
statelist.append(state.STATENAME)
list = widgets.SingleSelectField(options=statelist)

shday

unread,
Sep 7, 2007, 8:37:42 PM9/7/07
to TurboGears
>
> states = State.select()
> list = widgets.SingleSelectField(options=states)
>

I would use the SQL Construction Language instead of the ORM:

state_list = select([state_table.c.abbrev,state_table.c.name],
order_by=[state_table.c.abbrev]).execute()

state = widgets.SingleSelectField(options=state_list)

This syntax seems to have changed a bit in 0.4 though...

Steve

shday

unread,
Sep 7, 2007, 8:51:25 PM9/7/07
to TurboGears
oops, I assumed you where talking about SQLAlchemy. Please disregard
my comment.

Diez B. Roggisch

unread,
Sep 9, 2007, 6:42:21 AM9/9/07
to turbo...@googlegroups.com
badfrog schrieb:

No. And it's so trivial, that you can write it in a split-second
yourself, tailored to specific needs:

def prop_list(objects, attribute, sorted=True):
res = [(o.id, getattr(o, attribute)) for o in objects]
if sorted:
if not callable(sorted):
sorted = lambda v: v[1]
res.sort(key=sorted)
return res

Diez

Reply all
Reply to author
Forward
0 new messages