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 MultipleSelectField 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, primary_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'],
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.