@@ -85,7 +85,119 @@ Not yet documented. Please help us with new pull request.
8585
8686### BooleanField
8787
88- Not yet documented. Please help us with new pull request.
88+ - API: {class}` .db_fields.BooleanField `
89+ - Default form field class: {class}` ~.MongoBooleanField `
90+
91+ #### Form generation behaviour
92+
93+ BooleanField is very complicated in terms of Mongo database support. In
94+ Flask-Mongoengine before version ** 2.0.0+** database BooleanField used
95+ {class}` wtforms.fields.BooleanField ` as form representation, this raised several not
96+ clear problems, that was related to how {class}` wtforms.fields.BooleanField ` parse
97+ and work with form values. Known problems in version, before ** 2.0.0+** :
98+
99+ - Default value of field, specified in database definition was ignored, if default
100+ is ` None ` and nulls allowed, i.e. {attr}` null=True ` (Value was always ` False ` ).
101+ - Field was always created in database document, even if not checked, as there is
102+ impossible to split ` None ` and ` False ` values, when only checkbox available.
103+
104+ To fix all these issues, and do not create database field by default, Flask-Mongoengine
105+ ** 2.0.0+** uses dropdown field by default.
106+
107+ By default, database BooleanField not allowing ` None ` value, meaning that field can
108+ be ` True ` , ` False ` or not created in database at all. If database field configuration
109+ allowing ` None ` values, i.e. {attr}` null=True ` , then, when nothing selected in
110+ dropdown, the field will be created with ` None ` value.
111+
112+ ``` {important}
113+ It is responsobility of developer, to correctly setup database field definition and
114+ make proper tests before own application release. BooleanField can create unexpected
115+ application behavior in if checks. Developer, should recheck all if checks like:
116+
117+ - `if filed_value:` this will match `True` database value
118+ - `if not filed_value:` this will match `False` or `None` database value or not existing
119+ document key
120+ - `if field_value is None:` this will match `None` database value or not existing
121+ document key
122+ - `if field_value is True:` this will match `True` database value
123+ - `if field_value is False:` this will match `False` database value
124+ - `if field_value is not None:` this will match `True`, `False` database value
125+ - `if field_value is not True:` this will match `False`, `None` database value or not
126+ existing document key
127+ - `if filed_value is not False:` this will match `True`, `None` database value or not
128+ existing document key
129+ ```
130+
131+ #### Examples
132+
133+ ##### BooleanField with default dropdown
134+
135+ Such definition will not create any field in document, if dropdown not selected.
136+
137+ ``` python
138+ """ boolean_demo.py"""
139+ from example_app.models import db
140+
141+
142+ class BooleanDemoModel (db .Document ):
143+ """ Documentation example model."""
144+
145+ boolean_field = db.BooleanField()
146+ ```
147+
148+ ##### BooleanField with allowed ` None ` value
149+
150+ Such definition will create document field, even if nothing selected. The value will
151+ be ` None ` . If, during edit, ` yes ` or ` no ` dropdown values replaced to ` --- ` , then
152+ saved value in document will be aslo changed to ` None ` .
153+
154+ By default, ` None ` value represented as ` --- ` text in dropdown.
155+
156+ ``` python
157+ """ boolean_demo.py"""
158+ from example_app.models import db
159+
160+
161+ class BooleanDemoModel (db .Document ):
162+ """ Documentation example model."""
163+
164+ boolean_field_with_null = db.BooleanField(null = True )
165+ ```
166+
167+ ##### BooleanField with replaced dropdown text
168+
169+ Dropdown text can be easily replaced, there is only one requirement: New choices,
170+ should be correctly coerced by {func}` ~.coerce_boolean ` , or function should be
171+ replaced too.
172+
173+ ``` python
174+ """ boolean_demo.py"""
175+ from example_app.models import db
176+
177+
178+ class BooleanDemoModel (db .Document ):
179+ """ Documentation example model."""
180+
181+ boolean_field_with_as_choices_replace = db.BooleanField(
182+ wtf_options = {
183+ " choices" : [(" " , " Not selected" ), (" yes" , " Positive" ), (" no" , " Negative" )]
184+ }
185+ )
186+
187+ ```
188+
189+ ##### BooleanField with default ` True ` value, but with allowed nulls
190+
191+ ``` python
192+ """ boolean_demo.py"""
193+ from example_app.models import db
194+
195+
196+ class BooleanDemoModel (db .Document ):
197+ """ Documentation example model."""
198+
199+ true_boolean_field_with_allowed_null = db.BooleanField(default = True , null = True )
200+ ```
89201
90202### ComplexDateTimeField
91203
@@ -174,6 +286,8 @@ done, during field generation. Field is fully controllable by [global transforms
174286dates_demo.py in example app contain basic non-requirement example. You can adjust
175287it to any provided example for test purposes.
176288
289+ ##### Not limited DateField
290+
177291``` python
178292""" dates_demo.py"""
179293from example_app.models import db
@@ -185,12 +299,6 @@ class DateTimeModel(db.Document):
185299 date = db.DateField()
186300```
187301
188- ##### Not limited DateField
189-
190- ``` python
191- pass
192- ```
193-
194302### DateTimeField
195303
196304- API: {class}` .db_fields.DateTimeField `
0 commit comments