Am 28.05.2010 um 19:04 schrieb aspineux:


I need to fill in a combo with contacts,
these contacts depend of the service_id I get from the URL

I first define my own SingleSelectField class and define the
"update_params"

class ContactSelect(forms.SingleSelectField):

   def update_params(self, d):
       d['options']=tg.request.contact_list
       forms.SingleSelectField.update_params(self, d)

This is bad style, use

  super(ContactSelect, self).update_params(d)

in such cases. For a different solution in general, see below.

       return d

I use the tg.request.contact_list to initialize my list.
I have previously build this list in my controller

in my controller

   @expose('project.templates.monitor')
   def new(self, service_id, *args, **kwarg):
       ...
       tg.request.contact_list=get_contacts_for_service(service_id)
       ...
       return dict(
                 ....
                   )

This is simple and efficient but I dont like the way I do it !

- their could be a name conflict with other variable in tg.request.*
maybe I could use another tg or pylon "container" to store my
contact_list ?
- The ContactSelect class is not very easy to package

Any better way ?

Use the tmpl_context. And use a callable to options instead:

SingleSelect("contacts", options=lambda: tmpl_context.contacts_for_service)

@expose(...)
def new(...):
tmpl_context.contacts_for_service = get_contacts_for_service(service_id)


Diez

--
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