Defining the model for a formΒΆ
The underlying schema for a c2cgeoform
form is defined as a SQLAlchemy
model. A simple definition is shown below:
from sqlalchemy import (Column, Integer, Text)
import deform
from uuid import uuid4
from c2cgeoform.models import Base
class Comment(Base):
__tablename__ = 'comments'
__colanderalchemy_config__ = {
'title': 'A very simple form'
}
id = Column(Integer, primary_key=True, info={
'colanderalchemy': {
'widget': deform.widget.HiddenWidget()
}})
hash = Column(Text, unique=True, default=lambda: str(uuid4(), info={
'colanderalchemy': {
'widget': HiddenWidget()
}})
name = Column(Text, nullable=False, info={
'colanderalchemy': {
'title': 'Name'
}})
comment = Column(Text, nullable=True, info={
'colanderalchemy': {
'title': 'Comment',
'widget': deform.widget.TextAreaWidget(rows=3),
}})
This SQLAlchemy model is enriched with properties for ColanderAlchemy, for example to set a title for a field, use a specific Deform widget or use a Colander validator.
In general, every SQLAlchemy model can be used as schema for a form. The only requirements are:
- The model class must contain exactly one primary key column. Tables with composite primary keys are not supported.
A more complex example for a model can be found here. For more information on how to define the model, please refer to the SQLAlchemy, ColanderAlchemy, Colander and Deform documentations.