I need a dynamically filled SingleSelectField

83 views
Skip to first unread message

Moritz Schlarb

unread,
May 30, 2012, 1:06:59 PM5/30/12
to toscawidge...@googlegroups.com
Dear ToscaWidgetters,

I have a problem here and although threebean already tried to help me, it does not work yet!

This is the offending piece of code:
which just throws:
AttributeError: 'SubmissionForm_d' object has no attribute 'language_id'

It's a widget that needs the language_id selection field filled with values, but not with all values from the Database (then tw2.sqla.DbSelectionField would suffice) but with ones filtered by the SQLAlchemy query you see in the prepare method. It's dependent on the values the form gets displayed with, especially the associated assignment

In tw 0.9, I just prepared the options list for the SingleSelectField in my Controller and passed it via child_args to the widget, but that doesn't seem to work anymore!

If someone has a working solution for me, I'd be glad to get some advice or a fork of the gist!

Cheers,
Moritz

PS: Some older approaches by threebean and me: https://gist.github.com/2790405 and https://gist.github.com/2830475

Oliver

unread,
Apr 4, 2013, 11:06:02 AM4/4/13
to toscawidge...@googlegroups.com

I got the same problem and from the code point of view the easiest was for me was to deal with _iterate_options

I had to put a "self" in the lambda args which references the instance of DynamicSelectField...
 I hope this is useful for you


class DynamicSelectField(twf.SingleSelectField):
    def _iterate_options(self, optlist):
        if hasattr(optlist, '__call__'):
            for o in optlist():
                yield o
        else:
            for o in super(DynamicSelectField, self)._iterate_options(optlist):
                yield o

class ContentForm(twf.TableForm):
    id = twf.HiddenField
    page_id = DynamicSelectField(options=lambda self: ((p.id, p) for p in Page.query))


Paul Johnston

unread,
Apr 4, 2013, 11:07:22 AM4/4/13
to toscawidge...@googlegroups.com
Hi,

You may also find DbSingleSelectField in tw2.sqla useful for this.

Paul



--
You received this message because you are subscribed to the Google Groups "ToscaWidgets-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to toscawidgets-dis...@googlegroups.com.

To post to this group, send email to toscawidge...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Reply all
Reply to author
Forward
0 new messages