Dynamic select fields in a form

10 views
Skip to first unread message

Diego Woitasen

unread,
May 26, 2009, 3:18:49 PM5/26/09
to ToscaWidgets-discuss
Hi,
I want to build a form which have some select fields where they
values change in each instantiation of the form. My idea was to write
a subclass of TableForm, rewriting the __init__() function to get a
new parameter to filter select values. For example, I tried with this
but doesn't work (empty form):

class MyForm(TableForm):
def __init__(self, id=None, parent=None, children=[], filter = [],
**kw):
super(MyForm, self).__init__(id,parent,children, **kw)

options = get_data_from_db(filter)

self.fields = [
SelectionField(options = options)
]

For now I solved the problem writing I function that returns fields
and use it like this:

form = TableForm('name', action='foo', children = get_fields(filter))

but I don't like it.

Is there a way to write a subclass that takes some data to dynamically
change SelectionFields?

Regards,
Diego

percious

unread,
May 28, 2009, 6:47:40 PM5/28/09
to ToscaWidgets-discuss
I believe tw.dynforms has what you desire:

http://toscawidgets.org/documentation/tw.dynforms/

cheers.
-chris

Danny Adair

unread,
May 28, 2009, 6:57:42 PM5/28/09
to toscawidge...@googlegroups.com
Just use update_params or make options a callable.
See this thread:
http://markmail.org/message/pckkm2zjepfgxpjq

Cheers,
Danny
--
Kind regards,

Danny W. Adair
Director
Unfold Limited
New Zealand

Talk: +64 - 9 - 9555 101
Write: danny...@unfold.co.nz
Browse: www.unfold.co.nz
Visit/Post: 41 Hihitahi Rise, Te Haumi, Paihia 0200, New Zealand

"We are what we repeatedly do. Excellence, therefore, is not an act,
but a habit." - Aristotle

==============================
Caution
The contents of this email and any attachments contain information
which is CONFIDENTIAL to the recipient. If you are not the intended
recipient, you must not read, use, distribute, copy or retain this
email or its attachments. If you have received this email in error,
please notify us immediately by return email or collect telephone call
and delete this email. Thank you. We do not accept any
responsibility for any changes made to this email or any attachment
after transmission from us.
==============================

Diego Woitasen

unread,
Jun 3, 2009, 7:27:15 PM6/3/09
to ToscaWidgets-discuss


On May 28, 7:57 pm, Danny Adair <danny.ad...@unfold.co.nz> wrote:
> Just use update_params or make options a callable.
> See this thread:http://markmail.org/message/pckkm2zjepfgxpjq
>
> Cheers,
> Danny
>
>
>
> On Fri, May 29, 2009 at 10:47, percious <ch...@percious.com> wrote:
>
> > I believe tw.dynforms has what you desire:
>
> >http://toscawidgets.org/documentation/tw.dynforms/
>
> > cheers.
> > -chris
>
> Write:      danny.ad...@unfold.co.nz
> Browse:    www.unfold.co.nz
> Visit/Post: 41 Hihitahi Rise, Te Haumi, Paihia 0200, New Zealand
>
> "We are what we repeatedly do. Excellence, therefore, is not an act,
> but a habit." - Aristotle
>
> ==============================
> Caution
> The contents of this email and any attachments contain information
> which is CONFIDENTIAL to the recipient. If you are not the intended
> recipient, you must not read, use, distribute, copy or retain this
> email or its attachments. If you have received this email in error,
> please notify us immediately by return email or collect telephone call
> and delete this email.  Thank you.  We do not accept any
> responsibility for any changes made to this email or any attachment
> after transmission from us.
> ==============================

I followed the option 2 in the last mail (Alvaro's solution):

# pass options when displaying the form in the child_args dict
f.display(child_args=dict(age=dict(options=range(10))))

It's working for simpel fields but no for fieldsets. If I have a
fieldset called foo and a field called bar into it, how can I set
options for bar?

I tried with:

1) child_args = dict (bar = dict(options = bar_opts))
2) child_args = dict ('foo.bar' = dict(options = bar_opts))
3) child_args = dict (foo dict(bar = dict(options = bar_opts))

without success. No error, just doesn't work.

Regards,
Diego

Danny Adair

unread,
Jun 4, 2009, 4:34:39 PM6/4/09
to toscawidge...@googlegroups.com
On Thu, Jun 4, 2009 at 11:27, Diego Woitasen <die...@xtech.com.ar> wrote:
>[...]

> I followed the option 2 in the last mail (Alvaro's solution):
>
> # pass options when displaying the form in the child_args dict
> f.display(child_args=dict(age=dict(options=range(10))))
>
> It's working for simpel fields but no for fieldsets. If I have a
> fieldset called foo and a field called bar into it, how can I set
> options for bar?
>
> I tried with:
>
> 1) child_args = dict (bar = dict(options = bar_opts))
> 2) child_args = dict ('foo.bar' = dict(options = bar_opts))
> 3) child_args = dict (foo dict(bar = dict(options = bar_opts))
>
> without success. No error, just doesn't work.

the structure of the values follows the structure of the form, so it's

child_args = dict(foo=dict(bar=dict(options=bar_options)))

or more visually

child_args = {
'foo': {
'bar': {
'options': bar_options
}
}
}

Hth,
Danny

Diego Woitasen

unread,
Jun 7, 2009, 4:02:15 PM6/7/09
to ToscaWidgets-discuss


On 4 jun, 17:34, Danny Adair <danny.ad...@unfold.co.nz> wrote:
No, it doesn't work but I've found the right syntax:

child_args = { 'foo' :
{ 'child_args' =
{
'bar' : dict(options =
bar_options ),
}
}

Regards,
Diego
Reply all
Reply to author
Forward
0 new messages