Update SingleSelectField

7 views
Skip to first unread message

Eric

unread,
Nov 14, 2008, 2:58:05 AM11/14/08
to TurboGears
Hi,
Using TG 1.9.7a4, TG rookie. After submitting a form, I redirect back
to the same page to allow another entry.

raise redirect("new_ai")

There are SingleSelectField widgets whose values could change as a
result of the previous submission. What is the best way to force the
SingleSelectFields to reload?

def new_ai(self, **kw):
"""Form to add new record"""
pylons.c.form = create_aitracker_form
return dict(modelname='Action Item')

The TableForm class for the page has these lines:

people = DBSession.query(Person.id, Person.fullname).order_by
(Person.fullname).all()
assigned_by_id = SingleSelectField("Person_by", options = people,
default = 0, label_text = "Assigned by")

Thanks,
Eric

Eric

unread,
Nov 15, 2008, 3:37:06 PM11/15/08
to TurboGears
Hi all,
Update with what I tried (this all started with the tutorial code):

Create form...
create_aitracker_form = AITrackerForm("create_aitracker_form",
action='create_ai')

Then in the function that is called when the form is submitted I put:

global create_aitracker_form

and as a test I put create_aitracker_form = "" right before the
redirect

As expected, following the redirect I got an error, #TypeError: 'str'
object is not callable

However, when I changed the above create_aitracker_form line to:

create_aitracker_form = AITrackerForm("create_aitracker_form",
action='create_ai')

I get the original form without the updated single selects? I put
debug statements in the TableForm class and they are only executed
once when the page is first setup, not again when I update
create_aitracker_form. At this point I should admit that besides
being new to TG2 I am pretty new to Python so...

Thanks,
Eric

Diez B. Roggisch

unread,
Nov 16, 2008, 9:02:54 AM11/16/08
to turbo...@googlegroups.com

I'm not entirely sure what you are after here. Do you want the
SingleSelectField to be pointing to the value the user selected before
submitting the form?

Then don't use redirect. Redirect forces the browser to load the page from
scratch - so how should TG know what value to select?

Instead, use the @validate-decorator with the form, and a proper error_handler
that re-displays the form. It will then automatically get the user-filled-in
values.

Diez

Eric

unread,
Nov 17, 2008, 3:41:29 AM11/17/08
to TurboGears
Hi,
Thanks for the reply. The form has a SingleSelectField with names
that come from a database query. It also has a TextField to allow the
user to enter the name if the desired name isn't already in the
database/SingleSelectField. So if the user enters a new name in the
TextField, I would like the SingleSelectField to contain the name they
just entered after the redirect occurs (don't need to point to it, it
just needs to one of the selectable names). The SingleSelectField is
populated in the TableForm class and I can't seem to get the code to
rerun prior to the redirect. The correct table is being updated, if I
restart the application the new name shows up in the
SingleSelectField.

I pasted the code from the TableForm class and the functions for the
pages here:
http://paste.turbogears.org/paste/12620

Thanks,
Eric

Diez B. Roggisch

unread,
Nov 17, 2008, 3:57:17 AM11/17/08
to turbo...@googlegroups.com
>
> Hi,
> Thanks for the reply. The form has a SingleSelectField with names
> that come from a database query. It also has a TextField to allow the
> user to enter the name if the desired name isn't already in the
> database/SingleSelectField. So if the user enters a new name in the
> TextField, I would like the SingleSelectField to contain the name they
> just entered after the redirect occurs (don't need to point to it, it
> just needs to one of the selectable names). The SingleSelectField is
> populated in the TableForm class and I can't seem to get the code to
> rerun prior to the redirect. The correct table is being updated, if I
> restart the application the new name shows up in the
> SingleSelectField.
>
> I pasted the code from the TableForm class and the functions for the
> pages here:
> http://paste.turbogears.org/paste/12620


The problem is that you query your datasets while *constructing* the
widget. Which is of course only run once.

What you should do instead is passing callables to your SSFs, like this:


def get_projects():
return [(p.id, p.name) for p in Project.query.all()]


...

SingleSelectField(options=get_projects)


That ensures that the list of actual options is determined on render-time.

Diez

Eric

unread,
Nov 18, 2008, 1:54:44 AM11/18/08
to TurboGears
Hi,
Thanks yet again, that worked. I only get to play with this late at
night so I haven't gotten the basic flow of how all the parts play
together.

Thanks,
Eric
Reply all
Reply to author
Forward
0 new messages