1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
|
Form customization
==================
Custom fields
-------------
If you want to use a custom field class, you can pass it by using
form_field_class parameter for the column info dictionary.
Example ::
class User(Base):
__tablename__ = 'user'
name = sa.Column(sa.Unicode(100), primary_key=True, nullable=False)
color = sa.Column(
sa.String(7),
info={'form_field_class': ColorField},
nullable=False
)
class UserForm(ModelForm):
class Meta:
model = User
Now the 'color' field of UserForm would be a custom ColorField.
Forcing the use of SelectField
------------------------------
Sometimes you may want to have integer and unicode fields convert to SelectFields.
Probably the easiest way to achieve this is by using choices parameter for the column
info dictionary.
Example ::
class User(Base):
__tablename__ = 'user'
name = sa.Column(sa.Unicode(100), primary_key=True, nullable=False)
age = sa.Column(
sa.Integer,
info={'choices': [(i, i) for i in xrange(13, 99)]},
nullable=False
)
class UserForm(ModelForm):
class Meta:
model = User
Here the UserForm would have two fields. One TextField for the name column and one
SelectField for the age column containing range of choices from 13 to 99.
Notice that WTForms-Alchemy is smart enough to use the right coerce function based on
the underlying column type, hence in the previous example the age column would convert
to the following SelectField. ::
SelectField('Age', coerce=int, choices=[(i, i) for i in xrange(13, 99)])
For nullable unicode and string columns WTForms-Alchemy uses special null_or_unicode
coerce function, which converts empty strings to None values.
Field descriptions
------------------
Example::
class User(Base):
__tablename__ = 'user'
name = sa.Column(sa.Unicode(100), primary_key=True, nullable=False)
email = sa.Column(
sa.Unicode(255),
nullable=False,
info={'description': 'This is the description of email.'}
)
class UserForm(ModelForm):
class Meta:
model = User
Now the 'email' field of UserForm would have description 'This is the description of email.'
Field labels
------------
Example::
class User(Base):
__tablename__ = 'user'
name = sa.Column(
sa.Unicode(100), primary_key=True, nullable=False,
info={'label': 'Name'}
)
class UserForm(ModelForm):
class Meta:
model = User
Now the 'name' field of UserForm would have label 'Name'.
Custom widgets
--------------
Example::
from wtforms import widgets
class User(Base):
__tablename__ = 'user'
name = sa.Column(
sa.Unicode(100), primary_key=True, nullable=False,
info={'widget': widgets.HiddenInput()}
)
class UserForm(ModelForm):
class Meta:
model = User
Now the 'name' field of UserForm would use HiddenInput widget instead of TextInput.
Default values
--------------
By default WTForms-Alchemy ModelForm assigns the default values from column definitions.
Example ::
class User(Base):
__tablename__ = 'user'
name = sa.Column(sa.Unicode(100), primary_key=True, nullable=False)
level = sa.Column(sa.Integer, default=1)
class UserForm(ModelForm):
class Meta:
model = User
Now the UseForm 'level' field default value would be 1.
|