From 1e6a3163afc0bfc30a003010596a6800620d32a6 Mon Sep 17 00:00:00 2001 From: DavidBord Date: Tue, 5 Aug 2014 17:29:14 +0300 Subject: [PATCH] fix-#620: saving document doesn't create new fields in existing collection --- mongoengine/base/document.py | 2 ++ tests/document/instance.py | 10 ++++++++++ 2 files changed, 12 insertions(+) diff --git a/mongoengine/base/document.py b/mongoengine/base/document.py index 6f17202b5..d082f5c06 100644 --- a/mongoengine/base/document.py +++ b/mongoengine/base/document.py @@ -651,6 +651,8 @@ def _from_son(cls, son, _auto_dereference=True, only_fields=[]): default = default() if isinstance(default, BaseDocument): changed_fields.append(field_name) + elif not only_fields or field_name in only_fields: + changed_fields.append(field_name) if errors_dict: errors = "\n".join(["%s - %s" % (k, v) diff --git a/tests/document/instance.py b/tests/document/instance.py index a639fd797..c66fe0262 100644 --- a/tests/document/instance.py +++ b/tests/document/instance.py @@ -2633,5 +2633,15 @@ class Person(Document): p3 = Person.objects().only('created_on')[0] self.assertEquals(orig_created_on, p3.created_on) + class Person(Document): + created_on = DateTimeField(default=lambda: datetime.utcnow()) + name = StringField() + height = IntField(default=189) + + p4 = Person.objects()[0] + p4.save() + self.assertEquals(p4.height, 189) + self.assertEquals(Person.objects(height=189).count(), 1) + if __name__ == '__main__': unittest.main()