File: column_conversion.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 (208 lines) | stat: -rw-r--r-- 6,972 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
Column to form field conversion
===============================

Basic type conversion
---------------------

By default WTForms-Alchemy converts SQLAlchemy model columns using the following
type table. So for example if an Unicode column would be converted to TextField.

The reason why so many types here convert to wtforms_components based fields is that
wtforms_components provides better HTML5 compatible type handling than WTForms at the moment.


====================================    =================
 **SQAlchemy column type**              **Form field**
------------------------------------    -----------------
    BigInteger                          wtforms_components.fields.IntegerField
    Boolean                             BooleanField
    Date                                wtforms_components.fields.DateField
    DateTime                            wtforms_components.fields.DateTimeField
    Enum                                wtforms_components.fields.SelectField
    Float                               FloatField
    Integer                             wtforms_components.fields.IntegerField
    Numeric                             wtforms_components.fields.DecimalField
    SmallInteger                        wtforms_components.fields.IntegerField
    String                              TextField
    Text                                TextAreaField
    Time                                wtforms_components.fields.TimeField
    Unicode                             TextField
    UnicodeText                         TextAreaField
====================================    =================


WTForms-Alchemy also supports many types provided by SQLAlchemy-Utils.


====================================    =================
 **SQAlchemy-Utils type**               **Form field**
------------------------------------    -----------------
    ArrowType                           wtforms_components.fields.DateTimeField
    ChoiceType                          wtforms_components.fields.SelectField
    ColorType                           wtforms_components.fields.ColorField
    CountryType                         wtforms_alchemy.fields.CountryType
    EmailType                           wtforms_components.fields.EmailField
    IPAddressType                       wtforms_components.fields.IPAddressField
    PasswordType                        wtforms.fields.PasswordField
    PhoneNumberType                     wtforms_components.fields.PhoneNumberField
    URLType                             wtforms_components.fields.StringField + URL validator
    UUIDType                            wtforms.fields.TextField + UUID validator
    WeekDaysType                        wtforms_components.fields.WeekDaysField
====================================    =================


====================================    =================
 **SQAlchemy-Utils range type**         **Form field**
------------------------------------    -----------------
    DateRangeType                       wtforms_components.fields.DateIntervalField
    DateTimeRangeType                   wtforms_components.fields.DateTimeIntervalField
    IntRangeType                        wtforms_components.fields.IntIntervalField
    NumericRangeType                    wtforms_components.fields.DecimalIntervalField
====================================    =================




Excluded fields
---------------
By default WTForms-Alchemy excludes a column from the ModelForm if one of the following conditions is True:
    * Column is primary key
    * Column is foreign key
    * Column is DateTime field which has default value (usually this is a generated value)
    * Column is of TSVectorType type
    * Column is set as model inheritance discriminator field


Using include, exclude and only
-------------------------------

If you wish the include some of the excluded fields described in the earlier chapter you can use the 'include' configuration parameter.


In the following example we include the field 'author_id' in the ArticleForm (by default it is excluded since it is a foreign key column).

::


    class Article(Base):
        __tablename__ = 'article'

        id = sa.Column(sa.Integer, primary_key=True, nullable=False)
        name = sa.Column(
            sa.Unicode(255),
            nullable=False
        )
        author_id = sa.Column(sa.Integer, sa.ForeignKey(User.id))
        author = sa.orm.relationship(User)


    class ArticleForm(Form):
        class Meta:
            include = ['author_id']


If you wish the exclude fields you can either use 'exclude' or 'only' configuration parameters. The recommended way is using only, since in most cases it is desirable to explicitly tell which fields the form should contain.

Consider the following model:

::


    class Article(Base):
        __tablename__ = 'article'

        id = sa.Column(sa.Integer, primary_key=True, nullable=False)
        name = sa.Column(
            sa.Unicode(255),
            nullable=False
        )
        content = sa.Column(
            sa.UnicodeText
        )
        description = sa.Column(
            sa.UnicodeText
        )


Now let's say we want to exclude 'description' from the form. This can be achieved as follows:

::


    class ArticleForm(Form):
        class Meta:
            exclude = ['description']


Or as follows (the recommended way):


::


    class ArticleForm(Form):
        class Meta:
            only = ['name', 'content']




Adding/overriding fields
------------------------

Example::

    from wtforms.fields import TextField, IntegerField
    from wtforms.validators import Email

    class User(Base):
        __tablename__ = 'user'

        name = sa.Column(sa.Unicode(100), primary_key=True, nullable=False)
        email = sa.Column(
            sa.Unicode(255),
            nullable=False
        )

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

        email = TextField(validators=[Optional()])
        age = IntegerField()

Now the UserForm would have three fields:
    * name, a required TextField
    * email, an optional TextField
    * age, IntegerField


Type decorators
---------------

WTForms-Alchemy supports SQLAlchemy TypeDecorator based types. When WTForms-Alchemy encounters a TypeDecorator typed column it tries to convert it to underlying type field.

Example::


    import sqlalchemy as sa
    from wtforms.fields import TextField, IntegerField
    from wtforms.validators import Email


    class CustomUnicodeType(sa.types.TypeDecorator):
        impl = sa.types.Unicode

    class User(Base):
        __tablename__ = 'user'

        id = sa.Column(sa.Integer, primary_key=True, autoincrement=True)
        name = sa.Column(CustomUnicodeType(100), primary_key=True)


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


Now the name field of UserForm would be a simple TextField since the underlying type implementation is Unicode.