@@ -147,7 +147,7 @@ class BooleanDemoModel(db.Document):
147147
148148Such definition will create document field, even if nothing selected. The value will
149149be ` None ` . If, during edit, ` yes ` or ` no ` dropdown values replaced to ` --- ` , then
150- saved value in document will be aslo changed to ` None ` .
150+ saved value in document will be also changed to ` None ` .
151151
152152By default, ` None ` value represented as ` --- ` text in dropdown.
153153
@@ -407,7 +407,162 @@ class NumbersDemoModel(db.Document):
407407
408408## DictField
409409
410- Not yet documented. Please help us with new pull request.
410+ - API: {class}` .db_fields.DictField `
411+ - Default form field class: {class}` ~.MongoDictField `
412+
413+ DictField has ` Object ` type in terms of Mongo database itself, so basically it defines
414+ document inside document, but without pre-defined structure. That's why this is one
415+ of fields, that has default value specified inside Mongoengine itself, and that's
416+ why is always (almost) created.
417+
418+ The developer should understand that database keyword argument {attr}` default ` is
419+ forwarded to form by default, but can be separately overwritten in form. This brings
420+ a lot of options for form field configuration.
421+
422+ Also, should be additionally noted that database ` Null ` value in form is represented as
423+ empty string. Non-existing field is represented with form {attr}` default ` for new
424+ forms (without instance inside) or with empty string for non-empty forms.
425+
426+ Complicated? Probably. That's why this field was completely rewritten in version
427+ ** 2.0.0** . Check examples, and everything will be clear.
428+
429+ ### Form generation behaviour
430+
431+ Our default form generation follow Mongoengine internals and will use database field
432+ default (empty dict) to populate to new form or to not filled field in existing form.
433+
434+ In the same time, we are allowing extending of this behaviour, and not creating
435+ field in database, if default value provided as ` None ` . In this case, generated
436+ field for new form will be empty, without any pre-filled value.
437+
438+ Same empty field will be displayed in case, when both {attr}` default=None ` and
439+ {attr}` null=True ` selected, during database form initialization. In this case form
440+ field will be empty, without any placeholder, but on save ` null ` object will be
441+ created in document.
442+
443+ Also, we even support separated defaults for form field and database field, allowing
444+ any form+database behaviour.
445+
446+ ### Examples
447+
448+ #### DictField with default empty dict value
449+
450+ Will place ` {} ` to form for existing/new fields. This value is hardcodded in parent
451+ MongoEngine project.
452+
453+ ``` python
454+ """ dict_demo.py"""
455+ from example_app.models import db
456+
457+
458+ class DictDemoModel (db .Document ):
459+ """ Documentation example model."""
460+
461+ dict_field = db.DictField()
462+ ```
463+
464+ #### DictField with default ` None ` value, ignored by database
465+
466+ Reminder: Such field is empty in form, and will not create anything in database if
467+ not filled.
468+
469+ ``` python
470+ """ dict_demo.py"""
471+ from example_app.models import db
472+
473+
474+ class DictDemoModel (db .Document ):
475+ """ Documentation example model."""
476+
477+ no_dict_field = db.DictField(default = None )
478+ ```
479+
480+ #### DictField with default ` None ` value, saved to database
481+
482+ Reminder: Such field is empty in form, and will create ` null ` object in database if
483+ not filled.
484+
485+ ``` python
486+ """ dict_demo.py"""
487+ from example_app.models import db
488+
489+
490+ class DictDemoModel (db .Document ):
491+ """ Documentation example model."""
492+
493+ null_dict_field = db.DictField(default = None , null = True )
494+ ```
495+
496+ #### DictField with pre-defined default dict
497+
498+ This value is pre-defined on database level. So behaviour of form and in-code
499+ creation of such objects will be the same - default dict will be saved to database,
500+ if nothing provided to form/instance. Form will be pre-filled with default dict.
501+
502+ ``` python
503+ """ dict_demo.py"""
504+ from example_app.models import db
505+
506+
507+ def get_default_dict ():
508+ """ Example of default dict specification."""
509+ return {" alpha" : 1 , " text" : " text" , " float" : 1.2 }
510+
511+
512+ class DictDemoModel (db .Document ):
513+ """ Documentation example model."""
514+
515+ dict_default = db.DictField(default = get_default_dict)
516+ ```
517+
518+ #### DictField with pre-defined value and no document object creation on ` Null `
519+
520+ This is a case when you do not want to create any record in database document, if
521+ user completely delete pre-filled value in new document form. Here we use different
522+ ` null ` and ` default ` values in form field generation and during database object
523+ generation.
524+
525+ ``` python
526+ """ dict_demo.py"""
527+ from example_app.models import db
528+
529+
530+ def get_default_dict ():
531+ """ Example of default dict specification."""
532+ return {" alpha" : 1 , " text" : " text" , " float" : 1.2 }
533+
534+
535+ class DictDemoModel (db .Document ):
536+ """ Documentation example model."""
537+
538+ no_dict_prefilled = db.DictField(
539+ default = None ,
540+ null = False ,
541+ wtf_options = {" default" : get_default_dict, " null" : True },
542+ )
543+ ```
544+
545+ #### DictField with pre-defined default dict with ` Null ` fallback
546+
547+ This is very rare case, when some default value is given, meaning that this
548+ value will be populated to the field, but if completely deleted, than ` Null ` will be
549+ saved in database.
550+
551+ ``` python
552+ """ dict_demo.py"""
553+ from example_app.models import db
554+
555+
556+ def get_default_dict ():
557+ """ Example of default dict specification."""
558+ return {" alpha" : 1 , " text" : " text" , " float" : 1.2 }
559+
560+
561+ class DictDemoModel (db .Document ):
562+ """ Documentation example model."""
563+
564+ null_dict_default = db.DictField(default = get_default_dict, null = True )
565+ ```
411566
412567## EmailField
413568
0 commit comments