Skip to content

Instantly share code, notes, and snippets.

@witsch
Created February 27, 2012 22:14
Show Gist options
  • Save witsch/1927484 to your computer and use it in GitHub Desktop.
Save witsch/1927484 to your computer and use it in GitHub Desktop.
dynamic forms on deform
from colander import MappingSchema, SchemaNode, String
from deform import Form
def magnet(schema):
def decorator(node):
for child in node().children:
schema.add(child)
return node
return decorator
class CommentSchema(MappingSchema):
""" dynamically constructed schema """
schema = CommentSchema()
schema_magnet = magnet(schema)
# in this package...
@schema_magnet
class Comment(MappingSchema):
username = SchemaNode(String(), title='First name')
fullname = SchemaNode(String(), title='Last name')
email = SchemaNode(String())
title = SchemaNode(String())
text = SchemaNode(String())
# in some other package...
@schema_magnet
class Captcha(MappingSchema):
captcha = SchemaNode(String())
# again in this package...
form = Form(schema, button=['send'])
print form.render()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment