Merge pull request #1793 from erdenezul/insert_pymongo3
use insert_one, insert_many and remove deprecated one #1491
This commit is contained in:
commit
2cbdced974
@ -10,7 +10,9 @@ dev
|
|||||||
- Fix validation error instance in GenericEmbeddedDocumentField #1067
|
- Fix validation error instance in GenericEmbeddedDocumentField #1067
|
||||||
- Update cached fields when fields argument is given #1712
|
- Update cached fields when fields argument is given #1712
|
||||||
- Add a db parameter to register_connection for compatibility with connect
|
- Add a db parameter to register_connection for compatibility with connect
|
||||||
|
- Use insert_one, insert_many in Document.insert #1491
|
||||||
- Use new update_one, update_many on document/queryset update #1491
|
- Use new update_one, update_many on document/queryset update #1491
|
||||||
|
- Use insert_one, insert_many in Document.insert #1491
|
||||||
|
|
||||||
Changes in 0.15.0
|
Changes in 0.15.0
|
||||||
=================
|
=================
|
||||||
|
@ -350,11 +350,24 @@ class BaseQuerySet(object):
|
|||||||
documents=docs, **signal_kwargs)
|
documents=docs, **signal_kwargs)
|
||||||
|
|
||||||
raw = [doc.to_mongo() for doc in docs]
|
raw = [doc.to_mongo() for doc in docs]
|
||||||
|
|
||||||
|
with set_write_concern(self._collection, write_concern) as collection:
|
||||||
|
insert_func = collection.insert_many
|
||||||
|
if return_one:
|
||||||
|
raw = raw[0]
|
||||||
|
insert_func = collection.insert_one
|
||||||
|
|
||||||
try:
|
try:
|
||||||
ids = self._collection.insert(raw, **write_concern)
|
inserted_result = insert_func(raw)
|
||||||
|
ids = return_one and [inserted_result.inserted_id] or inserted_result.inserted_ids
|
||||||
except pymongo.errors.DuplicateKeyError as err:
|
except pymongo.errors.DuplicateKeyError as err:
|
||||||
message = 'Could not save document (%s)'
|
message = 'Could not save document (%s)'
|
||||||
raise NotUniqueError(message % six.text_type(err))
|
raise NotUniqueError(message % six.text_type(err))
|
||||||
|
except pymongo.errors.BulkWriteError as err:
|
||||||
|
# inserting documents that already have an _id field will
|
||||||
|
# give huge performance debt or raise
|
||||||
|
message = u'Document must not have _id value before bulk write (%s)'
|
||||||
|
raise NotUniqueError(message % six.text_type(err))
|
||||||
except pymongo.errors.OperationFailure as err:
|
except pymongo.errors.OperationFailure as err:
|
||||||
message = 'Could not save document (%s)'
|
message = 'Could not save document (%s)'
|
||||||
if re.match('^E1100[01] duplicate key', six.text_type(err)):
|
if re.match('^E1100[01] duplicate key', six.text_type(err)):
|
||||||
@ -368,7 +381,6 @@ class BaseQuerySet(object):
|
|||||||
signals.post_bulk_insert.send(
|
signals.post_bulk_insert.send(
|
||||||
self._document, documents=docs, loaded=False, **signal_kwargs)
|
self._document, documents=docs, loaded=False, **signal_kwargs)
|
||||||
return return_one and ids[0] or ids
|
return return_one and ids[0] or ids
|
||||||
|
|
||||||
documents = self.in_bulk(ids)
|
documents = self.in_bulk(ids)
|
||||||
results = []
|
results = []
|
||||||
for obj_id in ids:
|
for obj_id in ids:
|
||||||
|
@ -927,10 +927,6 @@ class QuerySetTest(unittest.TestCase):
|
|||||||
|
|
||||||
self.assertEqual(Blog.objects.count(), 2)
|
self.assertEqual(Blog.objects.count(), 2)
|
||||||
|
|
||||||
Blog.objects.insert([blog2, blog3],
|
|
||||||
write_concern={"w": 0, 'continue_on_error': True})
|
|
||||||
self.assertEqual(Blog.objects.count(), 3)
|
|
||||||
|
|
||||||
def test_get_changed_fields_query_count(self):
|
def test_get_changed_fields_query_count(self):
|
||||||
"""Make sure we don't perform unnecessary db operations when
|
"""Make sure we don't perform unnecessary db operations when
|
||||||
none of document's fields were updated.
|
none of document's fields were updated.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user