Skip to content

Commit d2fff08

Browse files
committed
fix efficiency of QuerySet.bulk_create()
1 parent 9688849 commit d2fff08

File tree

4 files changed

+15
-12
lines changed

4 files changed

+15
-12
lines changed

.github/workflows/test-python.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ jobs:
7070
auth_tests.test_models.UserManagerTestCase
7171
backends.base.test_base.DatabaseWrapperTests
7272
basic
73+
bulk_create
7374
dates
7475
datetimes
7576
empty

django_mongodb/compiler.py

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -189,9 +189,7 @@ def get_collection(self):
189189
class SQLInsertCompiler(SQLCompiler):
190190
def execute_sql(self, returning_fields=None):
191191
self.pre_sql_setup()
192-
193-
# pk_field = self.query.get_meta().pk
194-
keys = []
192+
objs = []
195193
for obj in self.query.objs:
196194
field_values = {}
197195
for field in self.query.fields:
@@ -207,18 +205,16 @@ def execute_sql(self, returning_fields=None):
207205
)
208206

209207
field_values[field.column] = value
210-
# TODO: pass the key value through db converters (use pk_field).
211-
keys.append(self.insert(field_values, returning_fields=returning_fields))
212-
213-
return keys
208+
objs.append(field_values)
209+
return [self.insert(objs, returning_fields=returning_fields)]
214210

215211
@wrap_database_errors
216-
def insert(self, doc, returning_fields=None):
217-
"""Store a document using field columns as element names."""
212+
def insert(self, docs, returning_fields=None):
213+
"""Store a list of documents using field columns as element names."""
218214
collection = self.get_collection()
219215
options = self.connection.operation_flags.get("save", {})
220-
inserted_id = collection.insert_one(doc, **options).inserted_id
221-
return [inserted_id] if returning_fields else []
216+
inserted_ids = collection.insert_many(docs, **options).inserted_ids
217+
return inserted_ids if returning_fields else []
222218

223219

224220
class SQLDeleteCompiler(SQLCompiler):

django_mongodb/features.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
class DatabaseFeatures(BaseDatabaseFeatures):
55
supports_date_lookup_using_string = False
66
supports_foreign_keys = False
7+
supports_ignore_conflicts = False
78
# Not implemented: https://github.com/mongodb-labs/django-mongodb/issues/8
89
supports_json_field = False
910
# Not implemented: https://github.com/mongodb-labs/django-mongodb/issues/7
@@ -63,6 +64,10 @@ class DatabaseFeatures(BaseDatabaseFeatures):
6364
}
6465

6566
django_test_skips = {
67+
"Insert expressions aren't supported.": {
68+
"bulk_create.tests.BulkCreateTests.test_bulk_insert_now",
69+
"bulk_create.tests.BulkCreateTests.test_bulk_insert_expressions",
70+
},
6671
"Pattern lookups on UUIDField are not supported.": {
6772
"model_fields.test_uuid.TestQuerying.test_contains",
6873
"model_fields.test_uuid.TestQuerying.test_endswith",
@@ -82,6 +87,7 @@ class DatabaseFeatures(BaseDatabaseFeatures):
8287
"update.tests.AdvancedTests.test_update_transformed_field",
8388
},
8489
"AutoField not supported.": {
90+
"bulk_create.tests.BulkCreateTests.test_bulk_insert_nullable_fields",
8591
"lookup.tests.LookupTests.test_in_ignore_none_with_unhashable_items",
8692
"model_fields.test_autofield.AutoFieldTests",
8793
"model_fields.test_autofield.BigAutoFieldTests",

django_mongodb/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ def wrapper(self, *args, **kwargs):
8484

8585
# These are the operations that this backend uses.
8686
count_documents = logging_wrapper("count_documents")
87-
insert_one = logging_wrapper("insert_one")
87+
insert_many = logging_wrapper("insert_many")
8888
delete_many = logging_wrapper("delete_many")
8989
update_many = logging_wrapper("update_many")
9090

0 commit comments

Comments
 (0)