Basic Forms

class Form(value=Unspecified, **kw)

Bases: flatland.schema.containers.Dict

A declarative collection of named fields.

Forms behave like flatland.Dict, but are defined with Python class syntax:

>>> from flatland import Form, String
>>> class HelloForm(Form):
...     hello = String
...     world = String

Fields are assigned names from the declaration. If a named schema is used, a renamed copy will be assigned to the Form.

>>> class HelloForm(Form):
...     hello = String.named('hello')   # redundant
...     world = String.named('goodbye') # will be renamed 'world'
>>> form = HelloForm()
>>> sorted(form.keys())
[u'hello', u'world']

Forms may embed other container fields and other forms:

>>> from flatland import List
>>> class BigForm(Form):
...     main_hello = HelloForm
...     alt_hello = List.of(String.named('alt_name'),
...                         HelloForm.named('alt_hello'))

This would create a form with one HelloForm embedded as main_hello, and a list of zero or more dicts, each containing an alt_name and another HelloForm named alt_hello.

Forms may inherit from other Forms or Dicts. Field declared in a subclass will override those of a superclass. Multiple inheritance is supported.

The special behavior of Form is limited to class construction time only. After construction, the Form acts exactly like a Dict. In particular, fields declared in class attribute style do not remain class attributes. They are removed from the class dictionary and placed in the field_schema:

>>> hasattr(HelloForm, 'hello')
>>> sorted([ for field in HelloForm.field_schema])
[u'hello', u'world']

The order of field_schema after construction is undefined.