开发者

How does one pre-populate a Python Formish form?

开发者 https://www.devze.com 2022-12-28 16:45 出处:网络
How does one pre-populate a Formish form? The obvious method as per t开发者_JAVA百科he documentation doesn\'t seem right. Using one of the provided examples:

How does one pre-populate a Formish form? The obvious method as per t开发者_JAVA百科he documentation doesn't seem right. Using one of the provided examples:

import formish, schemaish
structure = schemaish.Structure()
structure.add( 'a', schemaish.String() )
structure.add( 'b', schemaish.Integer() )
schema = schemaish.Structure()
schema.add( 'myStruct', structure )
form = formish.Form(schema, 'form')

If we pass this a valid request object:

form.validate(request)

The output is a structure like this:

{'myStruct': {'a': 'value', 'b': 0 }}

However, pre-populating the form using defaults requires this:

form.defaults = {'myStruct.a': 'value', 'myStruct.b': 0}

The dottedish package has a DottedDict object that can convert a nested dict to a dotted dict, but this asymmetry doesn't seem right. Is there a better way to do this?


No, don't require to use dotted dict, you can easily use the post-validate style dict to pre-populate the form:

form.defaults={'myStruct': {'a': None, 'b': 'default_value'}}

maybe have old version of formish, try update the libs.

0

精彩评论

暂无评论...
验证码 换一张
取 消