Your appstruct needs the same type of object your SchemaNode defines
containing just the "keys". For example if you want to set keys 1 and 3
as selected when the appstruct is rendered, pass:

'categories': set(['1', '3'])

Again, tested against the demo [0]

HTH.

[0] http://pastebin.com/8HpeEEvM

El 20/08/14 a las 09:12, Derek Hildreth escibió:
> Thank you, that seems like the sort of small thing I'd be missing, but
> it /still/ didn't work?
> 
> Recap:
> 
>     def get_category_choices():
>         all_categories = DBSession.query(Category).all()
>         return [(str(c.id <http://c.id/>), c.name <http://c.name/>) for
>     c in all_categories]
> 
>     categories = get_category_choices()
> 
>     class ProductForm(colander.Schema):
> 
>         categories = colander.SchemaNode(
>                     colander.Set(),
>                     widget=deform.widget.SelectWidget(
> 
>                         values=categories,
>                         multiple=True,
> 
>                     ),
> 
>                     default='3',                
> 
>                     validator=colander.Length(min=1),
>                     )
> 
> 
> My appstruct looks like:
> 
>     'categories': [{'id': '3', 'name': 'Category 3'}]
> 
> 
> The HTML generated does not contain selected attribute:
> 
>     <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>
> 
> 
> Mariano mentions setting it at render time... I suppose, but the deform
> demo here
> <http://deformdemo.repoze.org/sequence_of_defaulted_selects_with_initial_item/>
>  clearly
> demonstrates the ability to set defaults within the SchemaNode (which I
> was doing wrong before; thanks @replaceafill).
> 
> On Wednesday, August 20, 2014 8:55:05 AM UTC-6, replaceafill wrote:
> 
>     I think you have just set the default parameter incorrectly. It should
>     go on the SchemaNode [0]. You have set it on the SelectWidget [1].
> 
>     I tested it with the current deformdemo branch [2] and it seems to work
>     as you expect.
> 
>     HTH.
> 
>     [0]
>     
> http://docs.pylonsproject.org/projects/colander/en/latest/api.html#schema-related
>     
> <http://docs.pylonsproject.org/projects/colander/en/latest/api.html#schema-related>
> 
>     [1]
>     http://deform.readthedocs.org/en/latest/api.html#module-deform.widget 
> <http://deform.readthedocs.org/en/latest/api.html#module-deform.widget>
> 
>     [2] http://pastebin.com/EHcukwcT
> 
>     El 20/08/14 a las 08:35, Derek Hildreth escibió:
>     > Thanks so much for the response.  Unfortunately, it didn't add the
>     > 'selected' attribute to the select option.
>     >
>     > I tried the following as well, since you had a good point about value
>     > types and making sure they were strings...
>     >
>     >     return [(c <http://c.id/>.name, c.name <http://c.name>
>     <http://c.name/>) for c in
>     >     all_categories]
>     >
>     >
>     > And then,
>     >
>     >         categories = colander.SchemaNode(
>     >                     colander.Set(),
>     >                     widget=deform.widget.SelectWidget(
>     >
>     >                         values=categories,
>     >                         multiple=True,
>     >
>     >                         default='Category 3',
>     >
>     >                     ),
>     >                     validator=colander.Length(min=1),
>     >                     )
>     >
>     >
>     > I then made sure to update the appstruct similarly with str() so
>     that I
>     > end up with this in it:
>     >
>     >     'categories': [{'id': '3', 'name': 'Category 3'}]
>     >
>     >
>     > This didn't add the 'selected' attribute for the select option
>     either.
>     >
>     > Strange, right?
>     >
>     > On Wednesday, August 20, 2014 2:08:24 AM UTC-6, Rebelo wrote:
>     >
>     >
>     >     try replacing:
>     >     return [(c.id <http://c.id> <http://c.id>, c.name
>     <http://c.name> <http://c.name>) for c in
>     >     all_categories]
>     >     with
>     >
>     >     return [(str(c.id <http://c.id>) <http://c.id>, c.name
>     <http://c.name> <http://c.name>) for c in
>     >     all_categories]
>     >
>     >
>     >
>     >     On Wednesday, August 20, 2014 6:32:16 AM UTC+2, Derek Hildreth
>     wrote:
>     >
>     >         I have a schema defined as:
>     >
>     >             def get_category_choices():
>     >                 all_categories = DBSession.query(Category).all()
>     >                 return [(c.id <http://c.id> <http://c.id>, c.name
>     <http://c.name> <http://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/
>     
> <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] <javascript:>
>     > <mailto:[email protected] <javascript:>>.
>     > To post to this group, send email to [email protected]
>     <javascript:>
>     > <mailto:[email protected] <javascript:>>.
>     > Visit this group at http://groups.google.com/group/pylons-discuss
>     <http://groups.google.com/group/pylons-discuss>.
>     > For more options, visit https://groups.google.com/d/optout
>     <https://groups.google.com/d/optout>.
> 
> -- 
> 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]
> <mailto:[email protected]>.
> To post to this group, send email to [email protected]
> <mailto:[email protected]>.
> Visit this group at http://groups.google.com/group/pylons-discuss.
> For more options, visit https://groups.google.com/d/optout.

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

Reply via email to