presetting filtering fields

11 views
Skip to first unread message

Craig Small

unread,
Jan 10, 2013, 4:55:22 PM1/10/13
to tg-trunk
I'm hoping the list can "bridge the gap" between my controller and
widget. I have two problems:
A drop-down custom widget with options from a database field that
is filtered from sometinhg in the url
The widget "remembers" the last chosen option

So I have a widget much like
https://turbogears-dev.readthedocs.org/en/workshop/cookbook/sprox.html#creating-custom-dropdown-data
and it has ALL of the options from my table. By given the access of
http://localhost:8080/thing/42
my controller grabs the 42 so it knows item_id=42, so far so good.
My widget shows all genres, so far so good.

But I want my widget to only show the options filtered by this item, so
the database access would have filter(model.Genre.other_key == 42)
appended to it.

How do I get the item_id variable out of my controller into my widget?

Second problem, I select a genre and hit submit, I want that widget to
have the default of the one I selected instead of blank which is what it
does now. Again the controller "sees" the selection but its a matter of
letting the widget know.

- Craig
--
Craig Small VK2XLZ http://enc.com.au/ csmall at : enc.com.au
Debian GNU/Linux http://www.debian.org/ csmall at : debian.org
GPG fingerprint: 5D2F B320 B825 D939 04D2 0519 3938 F96B DF50 FEA5

Alessandro Molina

unread,
Jan 11, 2013, 4:18:43 AM1/11/13
to TurboGears .
This is a question better suited to the ToscaWidgets mailing list, so you might want to ask there for a better answer.

To set the currently selected genre simply pass to the form display call a value={} parameter that contains the key of the field and the value it should be set to.

To filter the Genre you have multiple options, the one I prefer is usually to override the prepare method of the form to perform such actions.
the prepare method is called right before displaying the widget, so you can change there any property depending on the context.

I usually do something like:
    def prepare(self):
        set_child_value(self, 'entry_type', 'options', [1,2,3])
        super(MyCustomForm, self).prepare()

where set_child_value is a function that looks like:

def set_child_value(w, childname, property, value):
    if w.child:
        w = w.child
    for c in w.children:
        if c.key == childname:
            setattr(c, property, value)
            break

maybe one of the tw2 guys has a better solution for this.


--
You received this message because you are subscribed to the Google Groups "TurboGears" group.
To post to this group, send email to turbo...@googlegroups.com.
To unsubscribe from this group, send email to turbogears+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/turbogears?hl=en.


Moritz Schlarb

unread,
Jan 11, 2013, 5:53:00 AM1/11/13
to turbo...@googlegroups.com, csm...@enc.com.au
Filtering the options in SelectWidgets? That's something, I'd need, too...
I guessed that the most generic way to do it would be to subclass sprox' SAORMProvider to accept arbitrary filter statementss.
I haven't had the time to implement that already, but percious told me on IRC that it would be possible

Craig Small

unread,
Jan 11, 2013, 6:35:19 AM1/11/13
to turbo...@googlegroups.com
On Fri, Jan 11, 2013 at 10:18:43AM +0100, Alessandro Molina wrote:
> This is a question better suited to the ToscaWidgets mailing list, so you
> might want to ask there for a better answer.
For the widget specific stuff yes, but currently I got this:
def prepare(self):
try:
self.value = int(tmpl_context.form_values['preset_time'])
except KeyError:
pass
super(GraphDatePresetWidget, self).prepare()

that tg.tmpl_context thing seems like a kludge, ideally id like value
found in the controller as its been run through the validator.
Reply all
Reply to author
Forward
0 new messages