From cd63865d311956f82396a5415f1f65f7d17cd654 Mon Sep 17 00:00:00 2001 From: Ross Lawley Date: Fri, 27 Jun 2014 09:08:07 +0100 Subject: [PATCH] Fix clear_changed_fields() clearing unsaved documents bug #602 --- AUTHORS | 1 + docs/changelog.rst | 1 + mongoengine/base/document.py | 12 +++++++----- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/AUTHORS b/AUTHORS index 2f832a3d..a0801b3c 100644 --- a/AUTHORS +++ b/AUTHORS @@ -196,3 +196,4 @@ that much better: * Polyrabbit (https://github.com/polyrabbit) * Sagiv Malihi (https://github.com/sagivmalihi) * Dmitry Konishchev (https://github.com/KonishchevDmitry) + * Martyn Smith (https://github.com/martynsmith) diff --git a/docs/changelog.rst b/docs/changelog.rst index 88959617..5c3ac1c1 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -6,6 +6,7 @@ Changelog Changes in 0.9.X - DEV ====================== +- Fix clear_changed_fields() clearing unsaved documents bug #602 - Removing support for Django 1.4.x, pymongo 2.5.x, pymongo 2.6.x. - Removing support for Python < 2.6.6 - Fixed $maxDistance location for geoJSON $near queries with MongoDB 2.6+ #664 diff --git a/mongoengine/base/document.py b/mongoengine/base/document.py index 08401823..f6eec628 100644 --- a/mongoengine/base/document.py +++ b/mongoengine/base/document.py @@ -16,7 +16,7 @@ from mongoengine.errors import (ValidationError, InvalidDocumentError, from mongoengine.python_support import PY3, txt_type from mongoengine.base.common import get_document, ALLOW_INHERITANCE -from mongoengine.base.datastructures import BaseDict, BaseList, StrictDict, SemiStrictDict +from mongoengine.base.datastructures import BaseDict, BaseList, StrictDict, SemiStrictDict from mongoengine.base.fields import ComplexBaseField __all__ = ('BaseDocument', 'NON_FIELD_ERRORS') @@ -54,12 +54,12 @@ class BaseDocument(object): values[name] = value __auto_convert = values.pop("__auto_convert", True) signals.pre_init.send(self.__class__, document=self, values=values) - + if self.STRICT and not self._dynamic: self._data = StrictDict.create(allowed_keys=self._fields.keys())() else: self._data = SemiStrictDict.create(allowed_keys=self._fields.keys())() - + self._dynamic_fields = SON() # Assign default values to instance @@ -150,7 +150,7 @@ class BaseDocument(object): try: self__initialised = self._initialised except AttributeError: - self__initialised = False + self__initialised = False # Check if the user has created a new instance of a class if (self._is_document and self__initialised and self__created and name == self._meta['id_field']): @@ -407,6 +407,8 @@ class BaseDocument(object): else: data = getattr(data, part, None) if hasattr(data, "_changed_fields"): + if hasattr(data, "_is_document") and data._is_document: + continue data._changed_fields = [] self._changed_fields = [] @@ -596,7 +598,7 @@ class BaseDocument(object): msg = ("Invalid data to create a `%s` instance.\n%s" % (cls._class_name, errors)) raise InvalidDocumentError(msg) - + if cls.STRICT: data = dict((k, v) for k,v in data.iteritems() if k in cls._fields) obj = cls(__auto_convert=False, **data)