hmarr/mongoengine — Issue #416: IntField with default value of 0 is lost on subsequent saves
Author: joeshaw
Created: 2012-01-27 04:09:36+00:00
State: open

Similar to BooleanFields with default values of False (see issue #282), IntFields with default values of 0 are dropped from the database when the originally created document is tweaked and saved.

Test code:

from mongoengine import *

connect("wolf")

class Doc(Document):
count = IntField(default=0)
name = StringField()

doc = Doc()

doc._get_collection().drop()

doc.save()

for d in doc._get_collection().find():
print d

# Uncomment this line, and things work fine
# doc.reload()

doc.name = "foo"
doc.save()

for d in doc._get_collection().find():
print d

This has output like this:

{u'count': 0, u'_types': [u'Doc'], u'_id': ObjectId('4f2223111725c673f6000000'), u'_cls': u'Doc'}
{u'_types': [u'Doc'], u'_id': ObjectId('4f2223111725c673f6000000'), u'_cls': u'Doc', u'name': u'foo'}

Note that `count` is missing from the latter.

If you uncomment the `doc.reload()` call, or query for the document any other way, things work as expected.