From 34e3e45843ef0475824b79b3405285a2d75d7648 Mon Sep 17 00:00:00 2001 From: Erdenezul Batmunkh Date: Thu, 8 Nov 2018 23:47:12 +0800 Subject: [PATCH 1/4] Use insert_one instead of deprecated one #1899 --- mongoengine/document.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/mongoengine/document.py b/mongoengine/document.py index fc379b61..37d60628 100644 --- a/mongoengine/document.py +++ b/mongoengine/document.py @@ -12,7 +12,11 @@ from mongoengine.base import (BaseDict, BaseDocument, BaseList, TopLevelDocumentMetaclass, get_document) from mongoengine.common import _import_class from mongoengine.connection import DEFAULT_CONNECTION_NAME, get_db -from mongoengine.context_managers import switch_collection, switch_db +from mongoengine.context_managers import ( + set_write_concern, + switch_collection, + switch_db +) from mongoengine.errors import (InvalidDocumentError, InvalidQueryError, SaveConditionError) from mongoengine.python_support import IS_PYMONGO_3 @@ -429,11 +433,11 @@ class Document(six.with_metaclass(TopLevelDocumentMetaclass, BaseDocument)): Helper method, should only be used inside save(). """ collection = self._get_collection() + with set_write_concern(collection, write_concern) as wc_collection: + if force_insert: + return wc_collection.insert_one(doc).inserted_id - if force_insert: - return collection.insert(doc, **write_concern) - - object_id = collection.save(doc, **write_concern) + object_id = wc_collection.insert_one(doc).inserted_id # In PyMongo 3.0, the save() call calls internally the _update() call # but they forget to return the _id value passed back, therefore getting it back here From 47577f2f47cfc6974baa795ff217f29351f919e8 Mon Sep 17 00:00:00 2001 From: Erdenezul Batmunkh Date: Fri, 9 Nov 2018 01:44:30 +0800 Subject: [PATCH 2/4] Update existing document #1899 --- mongoengine/document.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/mongoengine/document.py b/mongoengine/document.py index 37d60628..4abecaa9 100644 --- a/mongoengine/document.py +++ b/mongoengine/document.py @@ -436,6 +436,13 @@ class Document(six.with_metaclass(TopLevelDocumentMetaclass, BaseDocument)): with set_write_concern(collection, write_concern) as wc_collection: if force_insert: return wc_collection.insert_one(doc).inserted_id + # insert_one will provoke UniqueError alongside save does not + # therefore, it need to catch and call replace_one. + if '_id' in doc: + raw_object = wc_collection.find_one_and_replace( + {'_id': doc['_id']}, doc) + if raw_object: + return doc['_id'] object_id = wc_collection.insert_one(doc).inserted_id From 2a8579a6a50b88bbd5507746a93376f7c032b3fe Mon Sep 17 00:00:00 2001 From: Erdenezul Batmunkh Date: Fri, 9 Nov 2018 09:12:29 +0800 Subject: [PATCH 3/4] Update changelog #1899 --- docs/changelog.rst | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/docs/changelog.rst b/docs/changelog.rst index 222c6ea8..47af2b1b 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -5,7 +5,9 @@ Changelog Development =========== - (Fill this out as you fix issues and develop your features). -======= +- Remove deprecated `save()` method and used `insert_one()` #1899 + +================= Changes in 0.16.0 ================= - Various improvements to the doc From b47c5b5bfcad03d2b64ef8161f60dc288e28fd72 Mon Sep 17 00:00:00 2001 From: Erdenezul Batmunkh Date: Mon, 12 Nov 2018 09:53:39 +0800 Subject: [PATCH 4/4] Adhere imports into existing one #1899 --- mongoengine/document.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/mongoengine/document.py b/mongoengine/document.py index 4abecaa9..0945a8ed 100644 --- a/mongoengine/document.py +++ b/mongoengine/document.py @@ -12,11 +12,9 @@ from mongoengine.base import (BaseDict, BaseDocument, BaseList, TopLevelDocumentMetaclass, get_document) from mongoengine.common import _import_class from mongoengine.connection import DEFAULT_CONNECTION_NAME, get_db -from mongoengine.context_managers import ( - set_write_concern, - switch_collection, - switch_db -) +from mongoengine.context_managers import (set_write_concern, + switch_collection, + switch_db) from mongoengine.errors import (InvalidDocumentError, InvalidQueryError, SaveConditionError) from mongoengine.python_support import IS_PYMONGO_3