|
1 | | -# Flask-WTF(WTForms) integration |
| 1 | +# WTForms integration |
2 | 2 |
|
3 | 3 | ```{important} |
4 | 4 | Documentation below is related to project version 2.0.0 or higher, old versions has |
@@ -48,15 +48,21 @@ For all fields, processed by Flask-Mongoengine integration: |
48 | 48 | {attr}`validators` extension by Flask-Mongoengine. |
49 | 49 | - If model field definition have {attr}`wtf_filters` defined, they will be forwarded to |
50 | 50 | WTForm as {attr}`filters`. |
51 | | -- If model field definition have {attr}`required`, then {class}`~wtforms.validators.InputRequired` |
52 | | - will be added to form {attr}`validators`, otherwise {class}`~wtforms.validators.Optional` |
| 51 | +- If model field definition have {attr}`required`, then |
| 52 | + {class}`~wtforms.validators.InputRequired` |
| 53 | + will be added to form {attr}`validators`, otherwise |
| 54 | + {class}`~wtforms.validators.Optional` |
53 | 55 | added. |
54 | 56 | - If model field definition have {attr}`verbose_name` it will be used as form field |
55 | 57 | {attr}`label`, otherwise pure field name used. |
56 | 58 | - If model field definition have {attr}`help_text` it will be used as form field |
57 | 59 | {attr}`description`, otherwise empty string used. |
58 | | -- Field's {attr}`default` used as form {attr}`default`, that's why for string fields |
59 | | - special {class}`~.NoneStringField` with `None` value support used. |
| 60 | +- Field's {attr}`default` used as form {attr}`default`, that's why special WTForms |
| 61 | + fields implementations was created. Details can be found in |
| 62 | + {mod}`flask_mongoengine.wtf.fields` module. In new form generator only 'Mongo' |
| 63 | + prefixed classes are used for fields, other classes are deprecated and will be |
| 64 | + removed in version **3.0.0**. If you have own nesting classes, you should check |
| 65 | + inheritance and make an update. |
60 | 66 | - Field's {attr}`choices`, if exist, used as form {attr}`choices`. |
61 | 67 |
|
62 | 68 | ```{warning} |
@@ -99,7 +105,53 @@ Not yet documented. Please help us with new pull request. |
99 | 105 |
|
100 | 106 | ### EmailField |
101 | 107 |
|
102 | | -Not yet documented. Please help us with new pull request. |
| 108 | +- API: {class}`.db_fields.EmailField` |
| 109 | +- Default form field class: {class}`~.MongoEmailField` |
| 110 | + |
| 111 | +#### Form generation behaviour |
| 112 | + |
| 113 | +Unlike [StringField] WTForm class of the field is not adjusted by normal form |
| 114 | +generation sequence and always match {class}`~.MongoEmailField`. All other |
| 115 | +adjustments, related to validators insert are work with EmailField in the same way, |
| 116 | +as in [StringField]. |
| 117 | + |
| 118 | +Additional {class}`~wtforms.validators.Email` validator is also inserted to form |
| 119 | +field, to exclude unnecessary database request, if form data incorrect. |
| 120 | + |
| 121 | +Field respect user's adjustments in {attr}`wtf_field_class` option of |
| 122 | +{class}`.db_fields.EmailField`. This will change form field display, but will not |
| 123 | +change inserted validators. |
| 124 | + |
| 125 | +#### Examples |
| 126 | + |
| 127 | +strings_demo.py in example app contain basic non-requirement example. You can adjust |
| 128 | +it to any provided example for test purposes. |
| 129 | + |
| 130 | +##### Not required EmailField |
| 131 | + |
| 132 | +```python |
| 133 | +"""strings_demo.py""" |
| 134 | +from example_app.models import db |
| 135 | + |
| 136 | + |
| 137 | +class StringsDemoModel(db.Document): |
| 138 | + """Documentation example model.""" |
| 139 | + |
| 140 | + url_field = db.EmailField() |
| 141 | +```` |
| 142 | + |
| 143 | +##### Required EmailField |
| 144 | + |
| 145 | +```python |
| 146 | +"""strings_demo.py""" |
| 147 | +from example_app.models import db |
| 148 | + |
| 149 | + |
| 150 | +class StringsDemoModel(db.Document): |
| 151 | + """Documentation example model.""" |
| 152 | + |
| 153 | + required_url_field = db.EmailField(required=True) |
| 154 | +```` |
103 | 155 |
|
104 | 156 | ### EmbeddedDocumentField |
105 | 157 |
|
@@ -131,11 +183,215 @@ Not yet documented. Please help us with new pull request. |
131 | 183 |
|
132 | 184 | ### StringField |
133 | 185 |
|
134 | | -Not yet documented. Please help us with new pull request. |
| 186 | +- API: {class}`.db_fields.StringField` |
| 187 | +- Default form field class: Selected by field settings combination |
| 188 | + |
| 189 | +#### Form generation behaviour |
| 190 | + |
| 191 | +By default, during WTForm generation for fields without specified size ( |
| 192 | +{attr}`min_length` or {attr}`max_length`) class {class}`.MongoTextAreaField` is used, |
| 193 | +in case when {attr}`min_length` or {attr}`max_length` set, then |
| 194 | +{class}`.MongoStringField` used and {class}`~wtforms.validators.Length` will be added |
| 195 | +to form field validators. This allows to keep documents of any size in mongodb. |
| 196 | + |
| 197 | +In some cases class {class}`~.MongoStringField` is not the best choice for field, even |
| 198 | +with limited size. In this case user can easily overwrite generated field class by |
| 199 | +providing {attr}`wtf_field_class` on {class}`.db_fields.StringField` field declaration, |
| 200 | +as on document, as well as on form generation steps. |
| 201 | + |
| 202 | +If database field definition has {attr}`regex` parameter set, then |
| 203 | +{class}`~wtforms.validators.Regexp` validator will be added to the form field. |
| 204 | + |
| 205 | +#### Features deprecated |
| 206 | + |
| 207 | +Field declaration step keyword arguments {attr}`password` and {attr}`textarea` are |
| 208 | +deprecated in Flask-Mongoengine version **2.0.0** and exist only to make migration |
| 209 | +steps easy. |
| 210 | + |
| 211 | +To implement same behaviour, user should use {attr}`wtf_field_class` setting on |
| 212 | +{class}`.db_fields.StringField` init. |
| 213 | + |
| 214 | +#### Related WTForm custom fields |
| 215 | + |
| 216 | +Several special WTForms field implementation was created to support mongodb database |
| 217 | +behaviour and do not create any values in database, in case of empty fields. They |
| 218 | +can be used as {attr}`wtf_field_class` setting or independently. Some of them used |
| 219 | +in another database fields too, but all of them based on |
| 220 | +{class}`wtforms.fields.StringField` and {class}`~.EmptyStringIsNoneMixin`. You can use |
| 221 | +{class}`~.EmptyStringIsNoneMixin` for own field types. |
| 222 | + |
| 223 | +- {class}`~.MongoEmailField` |
| 224 | +- {class}`~.MongoHiddenField` |
| 225 | +- {class}`~.MongoPasswordField` |
| 226 | +- {class}`~.MongoSearchField` |
| 227 | +- {class}`~.MongoStringField` |
| 228 | +- {class}`~.MongoTelField` |
| 229 | +- {class}`~.MongoTextAreaField` |
| 230 | +- {class}`~.MongoURLField` |
| 231 | + |
| 232 | +#### Examples |
| 233 | + |
| 234 | +strings_demo.py in example app contain basic non-requirement example. You can adjust |
| 235 | +it to any provided example for test purposes. |
| 236 | + |
| 237 | +##### Not limited StringField as MongoTextAreaField |
| 238 | + |
| 239 | +```python |
| 240 | +"""strings_demo.py""" |
| 241 | +from example_app.models import db |
| 242 | + |
| 243 | + |
| 244 | +class StringsDemoModel(db.Document): |
| 245 | + """Documentation example model.""" |
| 246 | + |
| 247 | + string_field = db.StringField() |
| 248 | +``` |
| 249 | + |
| 250 | +##### Not limited StringField as MongoTelField |
| 251 | + |
| 252 | +```python |
| 253 | +"""strings_demo.py""" |
| 254 | +from example_app.models import db |
| 255 | +from flask_mongoengine.wtf import fields as mongo_fields |
| 256 | + |
| 257 | + |
| 258 | +class StringsDemoModel(db.Document): |
| 259 | + """Documentation example model.""" |
| 260 | + |
| 261 | + tel_field = db.StringField(wtf_field_class=mongo_fields.MongoTelField) |
| 262 | +``` |
| 263 | + |
| 264 | +##### Not limited StringField as MongoTextAreaField with https regex |
| 265 | + |
| 266 | +[mongoengine] and [wtforms] projects are not consistent in how they work with regex. |
| 267 | +You will be safe, if you use {func}`re.compile` each time, when you work with regex |
| 268 | +settings, before parent projects itself. |
| 269 | + |
| 270 | +```python |
| 271 | +"""strings_demo.py""" |
| 272 | +import re |
| 273 | + |
| 274 | +from example_app.models import db |
| 275 | + |
| 276 | + |
| 277 | +class StringsDemoModel(db.Document): |
| 278 | + """Documentation example model.""" |
| 279 | + |
| 280 | + regexp_string_field = db.StringField(regex=re.compile( |
| 281 | + r"^(https:\/\/)[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~:/?#[\]@!\$&'\(\)\*\+,;=]+$" |
| 282 | + )) |
| 283 | +``` |
| 284 | + |
| 285 | +##### Size limited StringField as MongoStringField |
| 286 | + |
| 287 | +```python |
| 288 | +"""strings_demo.py""" |
| 289 | +from example_app.models import db |
| 290 | + |
| 291 | + |
| 292 | +class StringsDemoModel(db.Document): |
| 293 | + """Documentation example model.""" |
| 294 | + |
| 295 | + sized_string_field = db.StringField(min_length=5) |
| 296 | +``` |
| 297 | + |
| 298 | +##### Required password field with minimum size |
| 299 | + |
| 300 | +```python |
| 301 | +"""strings_demo.py""" |
| 302 | +from example_app.models import db |
| 303 | +from flask_mongoengine.wtf import fields as mongo_fields |
| 304 | + |
| 305 | + |
| 306 | +class StringsDemoModel(db.Document): |
| 307 | + """Documentation example model.""" |
| 308 | + |
| 309 | + password_field = db.StringField( |
| 310 | + wtf_field_class=mongo_fields.MongoPasswordField, |
| 311 | + required=True, |
| 312 | + min_length=5, |
| 313 | + ) |
| 314 | +``` |
135 | 315 |
|
136 | 316 | ### URLField |
137 | 317 |
|
138 | | -Not yet documented. Please help us with new pull request. |
| 318 | +- API: {class}`.db_fields.URLField` |
| 319 | +- Default form field class: {class}`~.MongoURLField` |
| 320 | + |
| 321 | +#### Form generation behaviour |
| 322 | + |
| 323 | +Unlike [StringField] WTForm class of the field is not adjusted by normal form |
| 324 | +generation sequence and always match {class}`~.MongoURLField`. All other |
| 325 | +adjustments, related to validators insert are work with EmailField in the same way, |
| 326 | +as in [StringField]. |
| 327 | + |
| 328 | +Additional {class}`~wtforms.validators.Regexp` validator is also inserted to form |
| 329 | +field, to exclude unnecessary database request, if form data incorrect. This |
| 330 | +validator use regexp, provided in {attr}`url_regex` of {class}`.db_fields.URLField`, |
| 331 | +or default URL regexp from [mongoengine] project. This is different from |
| 332 | +Flask-Mongoengine version **1.0.0** or earlier, where {class}`~wtforms.validators.URL` |
| 333 | +was inserted. This was changed, to exclude validators conflicts. |
| 334 | + |
| 335 | +```{important} |
| 336 | +{func}`~.model_form` is still use {class}`~wtforms.validators.URL` for |
| 337 | +compatibility with old setups. |
| 338 | +``` |
| 339 | + |
| 340 | +Field respect user's adjustments in {attr}`wtf_field_class` option of |
| 341 | +{class}`.db_fields.URLField`. This will change form field display, but will not |
| 342 | +change inserted validators. |
| 343 | + |
| 344 | +#### Examples |
| 345 | + |
| 346 | +strings_demo.py in example app contain basic non-requirement example. You can adjust |
| 347 | +it to any provided example for test purposes. |
| 348 | + |
| 349 | +##### Not required URLField |
| 350 | + |
| 351 | +```python |
| 352 | +"""strings_demo.py""" |
| 353 | +from example_app.models import db |
| 354 | + |
| 355 | + |
| 356 | +class StringsDemoModel(db.Document): |
| 357 | + """Documentation example model.""" |
| 358 | + |
| 359 | + url_field = db.URLField() |
| 360 | +```` |
| 361 | + |
| 362 | +##### Required URLField with minimum size |
| 363 | + |
| 364 | +```python |
| 365 | +"""strings_demo.py""" |
| 366 | +from example_app.models import db |
| 367 | + |
| 368 | + |
| 369 | +class StringsDemoModel(db.Document): |
| 370 | + """Documentation example model.""" |
| 371 | + |
| 372 | + required_url_field = db.URLField(required=True, min_length=25) |
| 373 | +```` |
| 374 | + |
| 375 | +##### URLField with https only regexp check, if data exist |
| 376 | + |
| 377 | +Regexp for {attr}`url_regex` should be prepared by {mod}`re`. |
| 378 | + |
| 379 | +```python |
| 380 | +"""strings_demo.py""" |
| 381 | +import re |
| 382 | + |
| 383 | +from example_app.models import db |
| 384 | + |
| 385 | + |
| 386 | +class StringsDemoModel(db.Document): |
| 387 | + """Documentation example model.""" |
| 388 | + |
| 389 | + https_url_field = db.URLField( |
| 390 | + url_regex=re.compile( |
| 391 | + r"^(https:\/\/)[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~:/?#[\]@!\$&'\(\)\*\+,;=]+$" |
| 392 | + ), |
| 393 | + ) |
| 394 | +```` |
139 | 395 |
|
140 | 396 | ## Unsupported fields |
141 | 397 |
|
@@ -234,7 +490,15 @@ Not yet documented. Please help us with new pull request. |
234 | 490 | Not yet documented. Please help us with new pull request. |
235 | 491 |
|
236 | 492 | [mongoengine]: https://docs.mongoengine.org/ |
| 493 | + |
237 | 494 | [supported fields]: #supported-fields |
| 495 | + |
238 | 496 | [#379]: https://github.com/MongoEngine/flask-mongoengine/issues/379 |
| 497 | + |
239 | 498 | [integration]: forms |
| 499 | + |
240 | 500 | [global transforms]: #global-transforms |
| 501 | + |
| 502 | +[stringfield]: #stringfield |
| 503 | + |
| 504 | +[wtforms]: https://wtforms.readthedocs.io/en/3.0.x/ |
0 commit comments