Thanks Moritz, helped by your suggestion I managed to make it work.
I ended up doing it the following way:

def get_options(table, id_field):
    return [(getattr(rec, id_field), rec.__unicode__()) for rec in 
DBSession.query(table).all()]

class Proposer_id(twf.SingleSelectField):
    def prepare(self):
        self.options = get_options(Proposers_select_view, 'id')
        super(Proposer_id, self).prepare()

class BillController(CrudRestController):
...
    class Add_form(AddRecordForm):
        proposer_id = Proposer_id('proposer_id', label='Predlagatelji: ', 
options=[], size=8, validator=twc.Required)

I alsao tried to make it more general by overriding __init__ method of 
SingleSelectField widget (sending extra parameters to my __init__ and than 
calling SingleSelectField __init__ method but could not make it work).



On Friday, February 7, 2014 12:37:42 PM UTC+1, puska wrote:

> Hi everyone,
>
> I've been trying to create a form using CrudRestController which is having 
> TW2 SingleSelectField as a field of a column with foreign key on it. I 
> tried several approaches to get SingleSelectField to update it's options 
> per request, but my approaches do not work. My code looks like:
>
> from tgext.crud import CrudRestController
> from tgext.crud.utils import SortableTableBase
> import tw2.forms as twf
>
> def get_proposer_id_options():
>     return [(rec.id, rec.name) for rec in 
> DBSession.query(Proposers_select_view).all()]
>
> class BillController(CrudRestController):
>     class Table(SortableTableBase):
>         __model__ = Bills_view
>
>     class Table_filler(TableFiller):
>         __model__ = Bills_view
>
>     @expose('genshi:amandmani.templates.crud_new', inherit=True)
>     def new(self,  *args, **kw):
>         params = super(BillController, self).new(*args, **kw)
>         params['value'] = 
> dict(term_id=DBSession.query(Max_term_id_view).one().max_term_id,
>                                
> name=DBSession.query(Max_bill_view).one().max_bill+1)
>
>     class Add_form(AddRecordForm):
>         __model__ = Bills
>         proposer_id = twf.SingleSelectField('proposer_id',  
> label='Predlagatelj: ', options=get_proposer_id_options())
>
>     table = Table(DBSession)
>     table_filler = Table_filler(DBSession)
>     new_form = Add_form(DBSession)
>
> When I add a proposer into a database and refresh the "new" page 
> proposer_id SingleSelectField does not get updated.
> i also tried to use DbSingleSelectField from tw2.sqla but then I have 
> problems with setting initial values for the field (it needs to be an 
> instance of Porposers, not an id value) and validating values ( 
> DbSingleSelectField returns an instance of Porposers, not an id).
>
> I also noticed that it works out in admin but could not figure out how is 
> it accomplished there.
>
> Based on the activity of Turbogears group it looks like that either no one 
> is having that kind of a problem from 2011 (when TW2 was still 
> unavailable), so most probably I am doing something wrong.
> Any help would be greatly appreciated.
>
> Puska
>

-- 
You received this message because you are subscribed to the Google Groups 
"TurboGears" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/turbogears.
For more options, visit https://groups.google.com/d/optout.

Reply via email to