sqlschema
SQLObject-wrapping schema
The sqlschema module is accessible via the formencode module.
Classes
C SQLSchema(...) ...
SQLSchema objects are FormEncode schemas that are attached to specific instances or classes.
In .from_python(object) these schemas serialize SQLObject instances to dictionaries of values, or to empty dictionaries (when serializing a class). The object passed in should either be None (new or default object) or the object to edit.
In .to_python these either create new objects (when no id field is present) or edit an object by the included id. The returned value is the created object.
SQLObject validators are applied to the input, as is notNone restrictions. Also column restrictions and defaults are applied. Note that you can add extra fields to this schema, and they will be applied before the SQLObject validators and restrictions. This means you can use, for instance, validators.DateConverter() (assigning it to the same name as the SQLObject class's date column) to have this serialize date columns to/from strings.
You can override update_object to change the actual instantiation.
The basic idea is that a SQLSchema 'wraps' a class or instance (most typically a class). So it would look like:
class PersonSchema(SQLSchema):
wrap = Person
ps = PersonSchema()
form_defaults = ps.from_python(None)
new_object = ps.to_python(form_input)
form_defaults = ps.from_python(aPerson)
edited_person = ps.to_python(edited_form_input)
To override the encoding and decoding, use update_object and get_current. In this example, lets say that we take a single name field instead of a first_name and last_name (which is what the database has):
class PersonSchema(SQLSchema):
wrap = Person
def update_object(self, columns, extra, state):
name = extra.pop('name')
fname, lname = name.split(None, 1)
columns['first_name'] = fname
columns['last_name'] = lname
return super(PersonSchema).update_object(
columns, extra, state)
def get_current(self, obj, state):
value = super(PersonSchema).get_current(obj, state)
value['name'] = '%(first_name)s %(last_name)s' % value
del value['first_name']
del value['last_name']
Messages
- badID:
- The id %(value)r did not match the expected id
- badType:
- The input must be a string (not a %(type)s: %(value)r)
- empty:
- Please enter a value
- invalidID:
- The id is not valid: %(error)s
- missingValue:
- Missing value
- noneType:
- The input must be a string (not None)
- notExpected:
- The input field %(name)s was not expected.
- notNone:
- You may not provide None for that value
This class contains 37 members.
See the source for more information.