File: types.rst

package info (click to toggle)
wtforms-alchemy 0.19.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 492 kB
  • sloc: python: 3,955; makefile: 119; sh: 11
file content (333 lines) | stat: -rw-r--r-- 6,236 bytes parent folder | download | duplicates (3)
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
Type specific conversion
========================


Numeric type
------------

WTForms-Alchemy automatically converts Numeric columns to DecimalFields. The
converter is also smart enough to convert different decimal scales to
appropriate HTML5 input step args.


::


    class Account(Base):
        __tablename__ = 'event'

        id = sa.Column(sa.Integer, primary_key=True, autoincrement=True)
        balance = sa.Column(
            sa.Numeric(scale=2),
            nullable=False
        )

    class AccountForm(ModelForm):
        class Meta:
            model = Account


Now rendering AccountForm.balance would return the following HTML:

<input type='decimal' required step="0.01">


Arrow type
----------

WTForms-Alchemy supports the ArrowType of SQLAlchemy-Utils and converts it to
HTML5 compatible DateTimeField of WTForms-Components.

::


    from sqlalchemy_utils import ArrowType


    class Event(Base):
        __tablename__ = 'event'

        id = sa.Column(sa.Integer, primary_key=True, autoincrement=True)
        start_time = sa.Column(
            ArrowType(),
            nullable=False
        )

    class EventForm(ModelForm):
        class Meta:
            model = Event


Now the EventForm is essentially the same as:

::


    class EventForm(Form):
        start_time = DateTimeField(validators=[DataRequired()])


Choice type
-----------

WTForms-Alchemy automatically converts
:class:`sqlalchemy_utils.types.choice.ChoiceType` to WTForms-Components
SelectField.


::


    from sqlalchemy_utils import ChoiceType


    class Event(Base):
        __tablename__ = 'event'
        TYPES = [
            (u'party', u'Party'),
            (u'training, u'Training')
        ]

        id = sa.Column(sa.Integer, primary_key=True, autoincrement=True)
        type = sa.Column(ChoiceType(TYPES))


    class EventForm(ModelForm):
        class Meta:
            model = Event


Now the EventForm is essentially the same as:

::

    from wtforms_alchemy.utils import choice_type_coerce_factory


    class EventForm(Form):
        type = SelectField(
            choices=Event.TYPES,
            coerce=choice_type_coerce_factory(Event.type.type),
            validators=[DataRequired()]
        )



Color type
----------

::


    from sqlalchemy_utils import ColorType


    class CustomView(Base):
        __tablename__ = 'view'

        id = sa.Column(sa.Integer, primary_key=True, autoincrement=True)
        background_color = sa.Column(
            ColorType(),
            nullable=False
        )

    class CustomViewForm(ModelForm):
        class Meta:
            model = CustomView


Now the CustomViewForm is essentially the same as:

::


    from wtforms_components import ColorField


    class CustomViewForm(Form):
        color = ColorField(validators=[DataRequired()])



Country type
------------

::


    from sqlalchemy_utils import CountryType


    class User(Base):
        __tablename__ = 'user'

        id = sa.Column(sa.Integer, primary_key=True, autoincrement=True)
        country = sa.Column(CountryType, nullable=False)


    class UserForm(ModelForm):
        class Meta:
            model = User


The UserForm is essentially the same as:

::


    from wtforms_components import CountryField


    class UserForm(Form):
        country = CountryField(validators=[DataRequired()])



Email type
----------

::


    from sqlalchemy_utils import EmailType


    class User(Base):
        __tablename__ = 'user'

        id = sa.Column(sa.Integer, primary_key=True, autoincrement=True)
        email = sa.Column(EmailType, nullable=False)


    class UserForm(ModelForm):
        class Meta:
            model = User


The good old wtforms equivalent of this form would be:

::


    from wtforms_components import EmailField


    class UserForm(Form):
        email = EmailField(validators=[DataRequired()])



Password type
-------------

Consider the following model definition:

::


    from sqlalchemy_utils import PasswordType


    class User(Base):
        __tablename__ = 'user'

        id = sa.Column(sa.Integer, primary_key=True, autoincrement=True)
        name = sa.Column(sa.Unicode(100), nullable=False)
        password = sa.Column(
            PasswordType(
                schemes=['pbkdf2_sha512']
            ),
            nullable=False
        )

    class UserForm(ModelForm):
        class Meta:
            model = User


Now the UserForm is essentially the same as:

::

    class UserForm(Form):
        name = TextField(validators=[DataRequired(), Length(max=100)])
        password = PasswordField(validators=[DataRequired()])




Phonenumber type
----------------

WTForms-Alchemy supports the PhoneNumberType of SQLAlchemy-Utils and converts it automatically
to WTForms-Components PhoneNumberField. This field renders itself as HTML5 compatible phonenumber input.


Consider the following model definition:

::


    from sqlalchemy_utils import PhoneNumberType


    class User(Base):
        __tablename__ = 'user'

        id = sa.Column(sa.Integer, primary_key=True, autoincrement=True)
        name = sa.Column(sa.Unicode(100), nullable=False)
        phone_number = sa.Column(PhoneNumberType())


    class UserForm(ModelForm):
        class Meta:
            model = User


Now the UserForm is essentially the same as:

::

    from wtforms_components import PhoneNumberField


    class UserForm(Form):
        name = TextField(validators=[DataRequired(), Length(max=100)])
        phone_number = PhoneNumberField(validators=[DataRequired()])


URL type
--------

WTForms-Alchemy automatically converts SQLAlchemy-Utils URLType to StringField and adds URL validator for it.

Consider the following model definition:

::


    from sqlalchemy_utils import URLType


    class User(Base):
        __tablename__ = 'user'

        id = sa.Column(sa.Integer, primary_key=True, autoincrement=True)
        website = sa.Column(URLType())


    class UserForm(ModelForm):
        class Meta:
            model = User


Now the UserForm is essentially the same as:

::

    from wtforms_components import StringField
    from wtforms.validators import URL


    class UserForm(Form):
        website = StringField(validators=[URL()])