Skip to content

Commit

Permalink
chore: handle nested properties on insert (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
gersmann authored Dec 10, 2024
1 parent b986092 commit 97c94d0
Show file tree
Hide file tree
Showing 6 changed files with 219 additions and 200 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ repos:

- repo: https://github.com/charliermarsh/ruff-pre-commit
# Ruff version.
rev: "v0.3.5"
rev: "v0.8.2"
hooks:
- id: ruff
args: [--fix, --exit-non-zero-on-fix]
Expand Down
21 changes: 9 additions & 12 deletions django_mongodb/compiler.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from functools import cached_property
from itertools import chain

from dictlib import dug
from django.db.models.sql.compiler import (
SQLCompiler as BaseSQLCompiler,
)
Expand Down Expand Up @@ -240,25 +241,15 @@ def as_operation(self):
return {
"collection": opts.db_table,
"op": "insert_one",
"document": {
field.column: self.prepare_value(
field, self.pre_save_val(field, self.query.objs[0])
)
for field in fields
},
"document": self._fields_to_doc(fields, self.query.objs[0]),
}
elif self.returning_fields:
raise NotImplementedError(
"Returning fields is not supported for bulk inserts, right now, though very possible"
)
elif self.query.fields:
insert_statement = [
InsertOne(
{
field.column: self.prepare_value(field, self.pre_save_val(field, obj))
for field in fields
}
)
InsertOne(self._fields_to_doc(fields, obj))
if not obj.pk
# need to upsert if pk is given, because we support single collection inheritance
else UpdateOne(
Expand Down Expand Up @@ -287,6 +278,12 @@ def as_operation(self):
"requests": insert_statement,
}

def _fields_to_doc(self, fields, obj):
doc = {}
for field in fields:
dug(doc, field.column, self.prepare_value(field, self.pre_save_val(field, obj)))
return doc

def execute_sql(self, returning_fields=None):
assert not (
returning_fields
Expand Down
Loading

0 comments on commit 97c94d0

Please sign in to comment.