Hi everyone,

I am using TG 1.0.6 with SQlAlchemy 0.4.3. What I am working on at the
moment is to make a web app that will browse hierarchical data. In
order to do that I defined a form on which I show a question and
possible answers to that question in the form of SingleSelectField.
Therefore I need to select only the answers relevant to the question.
My controllers.py looks like this:

def rows(key_id):
    selected_rows=Zapisi.query.filter(Data.parent_id == key_id).all()
    rows_list=[]
    for selected_row in selected_rows:
        rows_list.append((selected_row.id, selected_row.answer))
    return rows_list


class DataSelectForm(TableForm):

     fields=[HiddenField('sifra_pitanja',
validator=validators.Int()),
                TextField('question',  label=_(u'Question')),
                SingleSelectField('answer_id',
validator=validators.Int(), label=_(u'Answers'),
options=rows(key_id))]
    submit_text=_(u'Select')

dataselect_form=DataSelectForm


class Root(controllers.RootController):

    def start(self, **form_data):
        try:
            key_id=int(form_data['answer_id'])
        except:
           key_id=1
        row=Data.query.filter(Data.id==key_id).first()
        value = {'answer_id' : '',
                    'question' : row.question}
        return dict(form=dataselect_form,
                    form_title='Browse data tree',
                    action='start',
                    value=value)
        raise redirect('/start')

and got:

SingleSelectField('answer_id', validator=validators.Int(),
label=_(u'Answers'), options=rows(key_id))]
NameError: name 'key_id' is not defined

then I tried defining key_id in the DataSelectForm class like this:

class DataSelectForm(TableForm):

     key_id=1
     fields=[HiddenField('sifra_pitanja',
validator=validators.Int()),
                TextField('question',  label=_(u'Question')),
                SingleSelectField('answer_id',
validator=validators.Int(), label=_(u'Answers'),
options=rows(key_id))]
    submit_text=_(u'Select')

and then

    def start(self, **form_data):
        try:
            key_id=int(form_data['answer_id'])
        except:
           key_id=1
        row=Data.query.filter(Data.id==key_id).first()
        value = {'answer_id' : '',
                    'question' : row.question}
        DataSelectForm.key_id=key_id
        dataselect_form=DataSelectForm()
        return dict(form=dataselect_form,
                    form_title='Browse data tree',
                    action='start',
                    value=value)
        raise redirect('/start')

But it just returns answers for key_id=1

Is it possible to work around this (i also tried __init__ approach but
got Name Error again)?
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"TurboGears" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/turbogears?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to