From 500eb920e4498c0573ceadd22f65117fd55fd075 Mon Sep 17 00:00:00 2001 From: Manuel Hermann Date: Tue, 7 Aug 2012 17:04:03 +0200 Subject: [PATCH] Use info from getlasterror whether a document has been updated or created. --- mongoengine/document.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/mongoengine/document.py b/mongoengine/document.py index f8bf769d..bb5a60fc 100644 --- a/mongoengine/document.py +++ b/mongoengine/document.py @@ -208,11 +208,20 @@ class Document(BaseDocument): actual_key = self._db_field_map.get(k, k) select_dict[actual_key] = doc[actual_key] + def is_new_object(last_error): + if last_error is not None: + updated = last_error.get("updatedExisting") + if updated is not None: + return not updated + return created + upsert = self._created if updates: - collection.update(select_dict, {"$set": updates}, upsert=upsert, safe=safe, **write_options) + last_error = collection.update(select_dict, {"$set": updates}, upsert=upsert, safe=safe, **write_options) + created = is_new_object(last_error) if removals: - collection.update(select_dict, {"$unset": removals}, upsert=upsert, safe=safe, **write_options) + last_error = collection.update(select_dict, {"$unset": removals}, upsert=upsert, safe=safe, **write_options) + created = created or is_new_object(last_error) cascade = self._meta.get('cascade', True) if cascade is None else cascade if cascade: