Skip to content

made QuerySet.bulk_create() set pk on created model instances #198

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions django_mongodb/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -677,13 +677,13 @@ def execute_sql(self, returning_fields=None):

field_values[field.column] = value
objs.append(field_values)
return [self.insert(objs, returning_fields=returning_fields)]
return self.insert(objs, returning_fields=returning_fields)

@wrap_database_errors
def insert(self, docs, returning_fields=None):
"""Store a list of documents using field columns as element names."""
inserted_ids = self.collection.insert_many(docs).inserted_ids
return inserted_ids if returning_fields else []
return [(x,) for x in inserted_ids] if returning_fields else []

@cached_property
def collection_name(self):
Expand Down
8 changes: 7 additions & 1 deletion django_mongodb/features.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class DatabaseFeatures(BaseDatabaseFeatures):
allows_multiple_constraints_on_same_fields = False
can_create_inline_fk = False
can_introspect_foreign_keys = False
can_return_rows_from_bulk_insert = True
greatest_least_ignores_nulls = True
has_json_object_function = False
has_native_json_field = True
Expand Down Expand Up @@ -197,7 +198,6 @@ def django_test_expected_failures(self):
},
"AutoField not supported.": {
"bulk_create.tests.BulkCreateTests.test_bulk_insert_nullable_fields",
"custom_pk.tests.CustomPKTests.test_auto_field_subclass_create",
"introspection.tests.IntrospectionTests.test_sequence_list",
"lookup.tests.LookupTests.test_filter_by_reverse_related_field_transform",
"lookup.tests.LookupTests.test_in_ignore_none_with_unhashable_items",
Expand All @@ -212,6 +212,12 @@ def django_test_expected_failures(self):
"model_fields.test_autofield.SmallAutoFieldTests",
"queries.tests.TestInvalidValuesRelation.test_invalid_values",
},
"Converters aren't run on returning fields from insert.": {
# Unsure this is needed for this backend. Can implement by request.
# https://github.com/django/django/commit/d9de74141e8a920940f1b91ed0a3ccb835b55729
"custom_pk.tests.CustomPKTests.test_auto_field_subclass_bulk_create",
"custom_pk.tests.CustomPKTests.test_auto_field_subclass_create",
},
"MongoDB does not enforce PositiveIntegerField constraint.": {
"model_fields.test_integerfield.PositiveIntegerFieldTests.test_negative_values",
},
Expand Down
Loading