ok, it look like I was too tired yesterday night :)
It doesn't work with default schema but with the deferred one it works
smoothly.
Sorry for the noise :P
Anyway, I wonder if this should not be a default behaviour: if the form
renderer find a 'request' element just try to get the value from there?
Wouldn't be useful?
On 10/04/2012 08:58 AM, Simone Orsi wrote:
> Hi all,
>
> I'm quite new to pyramid and deform and I'm trying to achieve
> something very simple that I suppose should work OOTB but I can't get
> it working.
>
> I have a simple schema-based form and I want the fields to get the
> selected value from the request once submitted.
>
> Here's the schema:
>
> class MySchema(colander.MappingSchema):
> department = colander.SchemaNode(
> colander.String(),
> default='',
> widget=SelectWidget(values=departments),
> )
>
> where choices come from an external db trough sqlalchemy. The form
> works but on every submit it lose selected value.
>
> So, I tried the deferred approach [1] like this:
>
> @colander.deferred
> def deferred_choices_widget(node, kw):
> choices = kw.get('choices')
> return SelectWidget(values=choices)
>
> @colander.deferred
> def deferred_default(node, kw):
> return kw['request'].POST.get('department')
>
> class MySchema(colander.MappingSchema):
> department = colander.SchemaNode(
> colander.String(),
> default=deferred_default,
> widget=deferred_choices_widget,
> )
>
> I noticed that the request was not in 'kw' so I binded it to the schema:
>
> schema = schema.bind(choices=departments,
> default=default,
> request=self.request)
>
> Now I see the value into deferred_default call and I can get it using
> `kw['request'].POST.get('department')` but there is no way to get it
> into the form on reload after submit.
>
> I can't find any meaningful step in the docs... what am I missing?
>
> Versions: colander-0.9.8-py2.7, deform-0.9.5-py2.7, pyramid-1.3.3-py2.7
>
> Thanks,
> SimO
>
> [1]
http://deformdemo.repoze.org/select_with_deferred/