Skip to content

Commit 78a9420

Browse files
author
erdenezul
authored
Merge pull request #1944 from erdenezul/deprecation_warning_pymongo37
Use insert_one instead of deprecated one #1899
2 parents 28a312a + b47c5b5 commit 78a9420

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

docs/changelog.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ Changelog
55
Development
66
===========
77
- (Fill this out as you fix issues and develop your features).
8-
=======
8+
- Remove deprecated `save()` method and used `insert_one()` #1899
9+
10+
=================
911
Changes in 0.16.0
1012
=================
1113
- Various improvements to the doc

mongoengine/document.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212
TopLevelDocumentMetaclass, get_document)
1313
from mongoengine.common import _import_class
1414
from mongoengine.connection import DEFAULT_CONNECTION_NAME, get_db
15-
from mongoengine.context_managers import switch_collection, switch_db
15+
from mongoengine.context_managers import (set_write_concern,
16+
switch_collection,
17+
switch_db)
1618
from mongoengine.errors import (InvalidDocumentError, InvalidQueryError,
1719
SaveConditionError)
1820
from mongoengine.python_support import IS_PYMONGO_3
@@ -426,11 +428,18 @@ def _save_create(self, doc, force_insert, write_concern):
426428
Helper method, should only be used inside save().
427429
"""
428430
collection = self._get_collection()
429-
430-
if force_insert:
431-
return collection.insert(doc, **write_concern)
432-
433-
object_id = collection.save(doc, **write_concern)
431+
with set_write_concern(collection, write_concern) as wc_collection:
432+
if force_insert:
433+
return wc_collection.insert_one(doc).inserted_id
434+
# insert_one will provoke UniqueError alongside save does not
435+
# therefore, it need to catch and call replace_one.
436+
if '_id' in doc:
437+
raw_object = wc_collection.find_one_and_replace(
438+
{'_id': doc['_id']}, doc)
439+
if raw_object:
440+
return doc['_id']
441+
442+
object_id = wc_collection.insert_one(doc).inserted_id
434443

435444
# In PyMongo 3.0, the save() call calls internally the _update() call
436445
# but they forget to return the _id value passed back, therefore getting it back here

0 commit comments

Comments
 (0)