Models API

FormAlchemy is aware of the __unicode__ and __html__ methods:

class User(Base):
    """A User model"""
    __tablename__ = 'users'
    id = Column(Integer, primary_key=True)
    email = Column(Unicode(40), unique=True, nullable=False)
    password = Column(Unicode(20), nullable=False)
    name = Column(Unicode(30))
    def __unicode__(self):
        """This is used to render the model in a relation field. Must return an
        unicode string."""
        return self.name
    def __html__(self):
        """This is used to render the model in relation field (readonly mode).
        You need need to clean up the html yourself. Use it at your own
        risk."""
        return '<a href="mailto:%s">%s</a>' % (self.email, self.name)
    def __repr__(self):
        return '<User %s>' % self.name

You can also use the formalchemy.Column() wrapper to set some extra options:

formalchemy.Column(*args, **kwargs)

Wrap the standard Column to allow to add some FormAlchemy options to a model field. Basically label and renderer but all the values are passed to set():

>>> from sqlalchemy import Integer
>>> from sqlalchemy.ext.declarative import declarative_base
>>> from formalchemy import Column
>>> Base = declarative_base()
>>> class MyArticle(Base):
...     __tablename__ = 'myarticles'
...     id = Column(Integer, primary_key=True, label='My id')
>>> MyArticle.__table__.c.id.info
{'label': 'My id'}

Previous topic

formalchemy – Imports

Next topic

formalchemy.fieldsFields and Renderers

This Page