Le 09/08/2011 21:43, MHCPU a écrit :
Damien,
Thanks for the reply, I really appreciate it. I'm a bit confused
though. In my template I was just passing in the whole ORM object:
<div py:replace="tmpl_context.sample_form(sample)">Input Form</div>
</div>
Is IdPrefilledData instead of "sample"?
sample is ok. But you must have a list of tuples (value, label).
And IdChildArgs seems to be the option list, but I already have that
from the initial definition of the Select. Do I need it again?
No you do not need it again. But if you fill it in the Form class
itself, it will not be updated dynamically. The forms are
instantiated once, so if you add new values in the database after
the first instantiation, then the values available for user choice
won't be updated.
And finally, the syntax you suggested, e.g. 'location': {'option 1',
'option 2', 'option 3'}} doesn't quite make sense -- the inner thing
looks like a list, but it has curly braces. What did you intend?
Example taken from my own source code:
${tmpl_context.sample_form(ldPrefilledData)}
with:
ldPrefilledData = {'f_status': {'options': (('1', 'option 1'),
('2', 'option 2'), ('3', 'option 3'))}}
Matthew
On Aug 9, 3:11 pm, Damien Accorsi <[email protected]> wrote:
Matthew,
In your templates you should have something like this:
${tmpl_context.sample_form(ldPrefilledData, child_args=ldChildArgs)}
Where :
* ldPrefilledData is a dictionnary containing the preselected values
* ldChildArgs is a dictionnary containing the available values
In your case, you should have something like:
* ldChildArgs = { 'location': {'option 1', 'option 2', 'option 3'}}
* ldPrefilledData = { 'location': {'option 1', 'option 2'}}
This should work.
Damien
Le 09/08/2011 20:40, MHCPU a écrit :I hope someone will tell me how to pass which options are selected into a MultipleSelectField. I can see that the values being passed into update_params is a list of SQLAlchemy ORM objects. It seems like it needs to be a list of strings instead, but I'm not sure how to make that happen. Matthew On Aug 9, 11:19 am, MHCPU<[email protected]>wrote:Could someone tell me how to get the selections in a MultipleSelectField to reflect the values coming from the database? The selections get saved to the database fine, but they don't get set in the form when I give the form an existing set of data. For example, I have a Sample table, with a one-to-many relation to the Location table. If the Location table has two records, with the "some_attribute" field having the values "option 1" and "option 2", I want those two options to be selected in the Multipl
eSelectField for "some_attribute" when I display the form. I'm not sure how the MultipleFieldSelect would even know that the data is supposed to come from the "some_attribute" field of the Location table, which is probably why it doesn't work. When I create the Sample object for insertion to the database, I have to make a list of Location objects and assign them to the new Sample. Do I have to do the reverse when I get a Sample from the database, in order to pass a list to the form? How would I pass it? Any help appreciated. - Matthew Tables: class Sample(DeclarativeBase): __tablename__ = 'sample' id = Column(Integer, primary_key=True) sampleNumber = Column(Unicode, index=True, unique=True, nullable=False) location = relation('Location', backref='sample', order_by='Location.id') class Location(DeclarativeBase): __tablename__ = 'location' id = Column(Integer, prima
ry_key=True) sample_id = Column(Unicode, ForeignKey('sample.id')) some_attribute = Column(Unicode) Form: class SampleForm(TableForm, twd.CustomisedForm): ... location = MultipleSelectField('some_attribute', options=['option 1', 'option 2', 'option 3']) Controller: class RootController(BaseController): @validate(sample_form.create_sample_form, error_handler=create) @expose() def create_sample(self, **kw): """Create a new sample record""" locationList = [] for ll in kw['location']: newLocation = Location(some_attribute=ll) locationList.append(newLocation) new = Sample( sampleNumber = kw['sampleNumber'], &nb
sp; location = locationList, ) DBSession.add(new) flash( '''Added sample: %s'''%( kw['sampleNumber'], )) redirect( './index' ) @expose('myproject.templates.update') def update(self, sampleNumber, **kw): """Display the form for updating a sample, with values from the database.""" tmpl_context.sample_form = sample_form.update_sample_form sample = DBSession.query(Sample).filter_by(sampleNumber=sampleNumber).one() return dict(sample=sample)
--
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.
|