TW: Loading options from the database

12 views
Skip to first unread message

Alex King

unread,
Jul 31, 2009, 9:08:24 PM7/31/09
to turbo...@googlegroups.com
Following the instructions at
http://www.turbogears.org/2.0/docs/main/ToscaWidgets/forms.html#loading-options-from-the-database,
I have my form loading options from the DB. Great.

Now I want to have a master/detail form (using tw.dynforms) where each
line has a field which needs to load options from the DB.

eg, in controller:

class LineForm(dynforms.GrowingTableFieldSet):
class children(core.WidgetsList):
amount = forms.TextField(validator=forms.validators.Number)
etype = forms.SingleSelectField()

class EntryForm(dynforms.CustomisedForm, forms.TableForm):
action = 'save_entry'
class children(core.WidgetsList):
date = forms.CalendarDatePicker()
description = forms.TextField()
elines = LineForm()
etype = forms.SingleSelectField()


I successfully pass options to the etype selector in my EntryForm
instance from the template:

${entryform(child_args=dict(etype=dict(options=topts)))}

but I can't find how to pass the same options to the etype selector in
the LineForm instance...

Can anyone point me in the right direction?

Cheers,
Alex

Diez B. Roggisch

unread,
Aug 1, 2009, 8:14:05 AM8/1/09
to turbo...@googlegroups.com
Alex King schrieb:

You need to pass child_args to the elines that contain child_args
themselves.

I'm not 100% sure because of the GrowingTableFieldSet, but it's either
directly a dict, or a list with a dict with one entry per row.


So either this

entryform.display(child_args=dict(...,
elines=dict(child_args=dict(etype=dict(...)))
)


or this:

entryform.display(child_args=dict(...,
elines=[dict(child_args=dict(etype=dict(...)), ...])
)

I presume the latter.

Diez

Alex King

unread,
Aug 1, 2009, 6:00:30 PM8/1/09
to TurboGears


On Aug 2, 12:14 am, "Diez B. Roggisch" <de...@web.de> wrote:
> Alex King schrieb:
>
>
>
> > Following the instructions at
> >http://www.turbogears.org/2.0/docs/main/ToscaWidgets/forms.html#loadi...,
Hmm,

The first results in no error, but the options are not loaded, and the
second a traceback. (Post traceback didn't seem to work.) The
traceback ends with:

Module tw.core.util:211 in unflatten_args
>> for key, val in v.iteritems():
AttributeError: 'list' object has no attribute 'iteritems'

So it looks like it doesn't like a list there. I guess I need to dig
in to the tw.dynforms code. I may try to see if I can build by own
repeating widget using standard wt.forms to try to understand this
better.

Thanks,
Alex

Alex King

unread,
Aug 1, 2009, 7:33:47 PM8/1/09
to TurboGears
In the end I gave up trying to pass in options during controler runs.
Instead I pass in a function when the GrowingTableFieldSet (LineForm)
is set up, and this works fine for my case:

def get_topts():
return DBSession.query(Ltype.id, Ltype.name)

class LineForm(dynforms.GrowingTableFieldSet):
class children(core.WidgetsList):
amount = forms.TextField(validator=forms.validators.Number)
etype = forms.SingleSelectField(options=get_topts)

class EntryForm(dynforms.CustomisedForm, forms.TableForm):
...

Diez B. Roggisch

unread,
Aug 2, 2009, 3:03:12 PM8/2/09
to turbo...@googlegroups.com
Alex King schrieb:

I had a look myself - and also have to admit being defeated. tw.dynforms
uses a rather complex widget-substructure that makes reaching down to
the actual children complicated, and I failed as well.

I will see if I can somehow make that easier.

Diez

Shane

unread,
Aug 28, 2009, 3:12:27 AM8/28/09
to TurboGears
Hello Diez,

I, too am in need of a way to load DB values into a SingleSelectField
within a GrowingTableFieldSet. I used Alex's method of passing a
function working easily on the initial load, but If I am loading a
series of existing entries (to edit an existing line item within an
order for instance, then I need to load data into the template using
some sort of child_arg passing. I was wondering if you had come up
with any method?

Thanks,

Shane

Diez B. Roggisch

unread,
Aug 28, 2009, 3:48:27 AM8/28/09
to turbo...@googlegroups.com
Shane schrieb:

> Hello Diez,
>
> I, too am in need of a way to load DB values into a SingleSelectField
> within a GrowingTableFieldSet. I used Alex's method of passing a
> function working easily on the initial load, but If I am loading a
> series of existing entries (to edit an existing line item within an
> order for instance, then I need to load data into the template using
> some sort of child_arg passing. I was wondering if you had come up
> with any method?

Unfortunately no, not so far. And I'm not sure if your use-case can be
tackled by this anyway.

The reason is that the growing table stuff works by creating a hidden
"template node", which is the one which determines for example the
options in a select-field.

Now adding rows will clone this node - and that of course always with
the default options.

Now if I understand you correctly, you want to have individual options,
per row - most probably based by some other field's value.

To make this work for all fields, you'd have to use Javascript then anyway.

Diez

Reply all
Reply to author
Forward
0 new messages