hmarr/mongoengine — Issue #440: Bug with boolean fields that have default of False
Author: maciel
Created: 2012-02-25 06:43:31+00:00
State: open

Try following code:

from mongoengine import *

connect('testproject')

class Foo(Document):
bool_field_with_default_false = BooleanField(default=False)

Foo.objects.delete()

f = Foo()
f.save()
print len(Foo.objects.filter(bool_field_with_default_false=False)) # prints 1

f.save()
print len(Foo.objects.filter(bool_field_with_default_false=False)) # prints 0

It seems that the second save should not have this effect. From my investigation the bug is in this line:

https://github.com/hmarr/mongoengine/blob/master/mongoengine/base.py#L796

The test should say:

if value != None

But this needs verification as the code suggests that some values should be unset if set to default value. Is that the case? it seems risky when you want to search by the default value as in the example above.