I have a schema defined as:
def get_category_choices():
all_categories = DBSession.query(Category).all()
return [(c.id, c.name) for c in all_categories]
categories = get_category_choices()
class ProductForm(colander.Schema):
name = colander.SchemaNode(colander.String(), title = "Name",
validator=colander.Length(max=80),
)
description = colander.SchemaNode(colander.String(),
title="Description",
validator=colander.Length(max=2000),
widget=deform.widget.TextAreaWidget(rows=10, cols=60),
)
categories = colander.SchemaNode(
colander.Set(),
widget=deform.widget.SelectWidget(
values=categories,
multiple=True,
),
validator=colander.Length(min=1),
)
Here's the code I'm using in Pyramid views.py:
myform = Form(ProductForm(), buttons=('submit',))
form = myform.render(product.appstruct())
return {'form': form}
My appstruct looks like this:
{'description': 'Product description goes here ', 'categories': [{'id': 1L,
'name': 'Category 1'}, {'id':3L, 'name':'Category 2'}], 'id': 1L, 'name':
'Product name goes here'}
I'd like the form to be rendered with a multiple select box that has
'Category 1' and 'Category 2' selected by default, like so:
<select name="categories" id="deformField4" multiple="multiple">
<option value="1" selected="selected">Category 1</option>
<option value="2">Category 2</option>
<option value="3" selected="selected">Category 3</option>
</select>
I can't seem to get this to happen. I have tried to use 'default' (as
shown deform demo here
<http://deformdemo.repoze.org/sequence_of_defaulted_selects_with_initial_item/>)
to get at least *one* item to select (for a test), and it doesn't seem to
do it either. In other words, this:
categories = colander.SchemaNode(
colander.Set(),
widget=deform.widget.SelectWidget(
values=categories,
multiple=True,
default='3',
),
validator=colander.Length(min=1),
)
Results in this (notice lack of 'selected' attributes):
<select name="categories" id="deformField4" multiple="multiple">
<option value="1">Category 1</option>
<option value="2">Category 2</option>
<option value="3">Category 3</option>
</select>
I must be missing something simple here. I'd really appreciate any help.
Thanks in advance.
--
You received this message because you are subscribed to the Google Groups
"pylons-discuss" 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/pylons-discuss.
For more options, visit https://groups.google.com/d/optout.