That works, but I need fields inside the constructor because I want to use
SelectionList or CheckboxList which contents could change in each use of
RuleForm (quering data from database).
I could use a function that returns the children field list and use them
like you said but I prefer to use classes or at least understand why my
approach doesn't work.
Regards,
Diego
-- Diego Woitasen
XTECH - Soluciones Linux para empresas
(54) 011 5219-0678
> On Tue, May 26, 2009 6:50 am, Diez B. Roggisch wrote: > > On Tuesday 26 May 2009 03:28:14 Diego Woitasen wrote: > >> Hi, > >> I want to write a reusable form using TableForm as base class. I tried > >> with this:
> On Tue, May 26, 2009 6:50 am, Diez B. Roggisch wrote: > > On Tuesday 26 May 2009 03:28:14 Diego Woitasen wrote: > >> Hi, > >> I want to write a reusable form using TableForm as base class. I tried > >> with this:
> That works, but I need fields inside the constructor because I want to use > SelectionList or CheckboxList which contents could change in each use of > RuleForm (quering data from database).
This is not intended to work. ToscaWidgets are instantiated once, and re-used.
To make selectfields display varying values, use a callable as options-parameter.
def get_options(): return some_options
class MyForm(TableForm): class fields(WidgetList): select_field = SingleSelectField(options=get_options)
On Tue, May 26, 2009 11:45 am, Diez B. Roggisch wrote:
> On Tuesday 26 May 2009 16:04:09 Diego Woitasen wrote:
>> On Tue, May 26, 2009 6:50 am, Diez B. Roggisch wrote:
>> > On Tuesday 26 May 2009 03:28:14 Diego Woitasen wrote:
>> >> Hi,
>> >> I want to write a reusable form using TableForm as base class. I
>> tried
>> >> with this:
On Tue, May 26, 2009 11:49 am, Diez B. Roggisch wrote:
> On Tuesday 26 May 2009 16:04:09 Diego Woitasen wrote:
>> On Tue, May 26, 2009 6:50 am, Diez B. Roggisch wrote:
>> > On Tuesday 26 May 2009 03:28:14 Diego Woitasen wrote:
>> >> Hi,
>> >> I want to write a reusable form using TableForm as base class. I
>> tried
>> >> with this:
>> > Diez
>> Means, it doesn't work. If I use fields outside the constructor works, for
>> example:
>> class RuleForm(TableForm):
>> fields = [
>> TextField(id = 'description', label = 'Description'),
>> ]
>> def __init__(self, id=None, parent=None, children=[], host_filter=[],
>> **kw):
>> super(RuleForm, self).__init__(id,parent,children, **kw)
>> That works, but I need fields inside the constructor because I want to use
>> SelectionList or CheckboxList which contents could change in each use
of
>> RuleForm (quering data from database).
> This is not intended to work. ToscaWidgets are instantiated once, and
re-used.
> To make selectfields display varying values, use a callable as
> options-parameter.
> def get_options():
> return some_options
> class MyForm(TableForm):
> class fields(WidgetList):
> select_field = SingleSelectField(options=get_options)
> Diez
I got "TypeError: 'function' object is not iterable".
Anyway, I don't like that approach. What if a want to pass parameters to
get_options()? I want to pass a SQLAlchemy query when the form is
instantiated to condition the data to be returned i select fields.
I think the solution at the moment is to write a function that return all
childrens to pass to my form but I don't like it.
I'm using TG2 RC1.
-- Diego Woitasen
XTECH - Soluciones Linux para empresas
(54) 011 5219-0678
> On Tue, May 26, 2009 11:49 am, Diez B. Roggisch wrote: >> On Tuesday 26 May 2009 16:04:09 Diego Woitasen wrote: >>> On Tue, May 26, 2009 6:50 am, Diez B. Roggisch wrote: >>>> On Tuesday 26 May 2009 03:28:14 Diego Woitasen wrote: >>>>> Hi, >>>>> I want to write a reusable form using TableForm as base class. I >>> tried >>>>> with this:
>>>>> without sucess. >>>> What does "without success" mean? You are aware, that in the latter > example >>>> you simply override whatever children are passed?
>>>> Besides, the simple problem of yours seems to be solvable using
>>>> Diez >>> Means, it doesn't work. If I use fields outside the constructor works, for >>> example: >>> class RuleForm(TableForm): >>> fields = [ >>> TextField(id = 'description', label = 'Description'), >>> ] >>> def __init__(self, id=None, parent=None, children=[], host_filter=[], >>> **kw): >>> super(RuleForm, self).__init__(id,parent,children, **kw) >>> That works, but I need fields inside the constructor because I want to use >>> SelectionList or CheckboxList which contents could change in each use > of >>> RuleForm (quering data from database). >> This is not intended to work. ToscaWidgets are instantiated once, and > re-used. >> To make selectfields display varying values, use a callable as >> options-parameter.
>> def get_options(): >> return some_options
>> class MyForm(TableForm): >> class fields(WidgetList): >> select_field = SingleSelectField(options=get_options)
>> Diez
> I got "TypeError: 'function' object is not iterable".
Full stacktrace please, and code.... crystal-ball time is over for this month.
This works in our codebase, so the problem must be on your side:
def get_users(): """ This function returns a list of tuples
(User, str)
to be used as options for a SingleSelectField.
It is important to work with **User**-objects here, **not** with ids! Otherwise in case of a failed validation, the display of the SingleSelectField wouldn't show the last selected value. """ return [(u, u.email) for u in User.query()[:10]]
> Anyway, I don't like that approach. What if a want to pass parameters to > get_options()? I want to pass a SQLAlchemy query when the form is > instantiated to condition the data to be returned i select fields.
> I think the solution at the moment is to write a function that return all > childrens to pass to my form but I don't like it.
There are various ways to overcome this. You can use the tmpl_context to pass the query.