@@ -243,7 +243,9 @@ class Dog(db.Document):
243243 class DogOwner (db .Document ):
244244 dog = db .ReferenceField (Dog )
245245
246- DogOwnerForm = model_form (DogOwner )
246+ DogOwnerForm = model_form (DogOwner , field_args = {
247+ 'dog' : { 'allow_blank' : True }
248+ })
247249
248250 dog = Dog (name = "fido" )
249251 dog .save ()
@@ -254,6 +256,15 @@ class DogOwner(db.Document):
254256 self .assertEqual (wtforms .widgets .Select , type (form .dog .widget ))
255257 self .assertFalse (form .dog .widget .multiple )
256258
259+ # Validate the options - should contain a dog (selected) and a
260+ # blank option there should be an extra blank option.
261+ choices = list (form .dog )
262+ self .assertEqual (len (choices ), 2 )
263+ self .assertFalse (choices [0 ].checked )
264+ self .assertEqual (choices [0 ].data , '__None' )
265+ self .assertTrue (choices [1 ].checked )
266+ self .assertEqual (choices [1 ].data , dog .pk )
267+
257268 # Validate selecting one item
258269 form = DogOwnerForm (MultiDict ({
259270 'dog' : dog .id ,
@@ -276,7 +287,9 @@ class Dog(db.Document):
276287 class DogOwner (db .Document ):
277288 dogs = db .ListField (db .ReferenceField (Dog ))
278289
279- DogOwnerForm = model_form (DogOwner )
290+ DogOwnerForm = model_form (DogOwner , field_args = {
291+ 'dogs' : { 'allow_blank' : True }
292+ })
280293
281294 dogs = [Dog (name = "fido" ), Dog (name = "rex" )]
282295 for dog in dogs :
@@ -288,11 +301,16 @@ class DogOwner(db.Document):
288301 self .assertEqual (wtforms .widgets .Select , type (form .dogs .widget ))
289302 self .assertTrue (form .dogs .widget .multiple )
290303
291- # Validate if both dogs are selected
304+ # Validate the options - both dogs should be selected and
305+ # there should be an extra blank option.
292306 choices = list (form .dogs )
293- self .assertEqual (len (choices ), 2 )
294- self .assertTrue (choices [0 ].checked )
307+ self .assertEqual (len (choices ), 3 )
308+ self .assertFalse (choices [0 ].checked )
309+ self .assertEqual (choices [0 ].data , '__None' )
295310 self .assertTrue (choices [1 ].checked )
311+ self .assertEqual (choices [1 ].data , dogs [0 ].pk )
312+ self .assertTrue (choices [2 ].checked )
313+ self .assertEqual (choices [2 ].data , dogs [1 ].pk )
296314
297315 # Validate selecting two items
298316 form = DogOwnerForm (MultiDict ({
@@ -302,7 +320,7 @@ class DogOwner(db.Document):
302320
303321 # Validate selecting none actually empties the list
304322 form = DogOwnerForm (MultiDict ({
305- 'dogs' : [] ,
323+ 'dogs' : '__None' ,
306324 }), dogs = dogs )
307325 self .assertEqual (form .dogs .data , None )
308326
0 commit comments