Skip to content

Commit 23b9bcc

Browse files
committed
1 parent dd0dde0 commit 23b9bcc

File tree

2 files changed

+17
-23
lines changed

2 files changed

+17
-23
lines changed

django_mongodb_engine/compiler.py

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,7 @@ def execute_sql(self, result_type=MULTI):
403403
ret.append(result)
404404
return ret
405405

406+
406407
class SQLInsertCompiler(NonrelInsertCompiler, SQLCompiler):
407408
@safe_call
408409
def insert(self, data, return_id=False):
@@ -412,39 +413,21 @@ def insert(self, data, return_id=False):
412413
pass
413414
return self._save(data, return_id)
414415

416+
415417
# TODO: Define a common nonrel API for updates and add it to the nonrel
416418
# backend base classes and port this code to that API
417419
class SQLUpdateCompiler(NonrelUpdateCompiler, SQLCompiler):
418420
query_class = MongoQuery
419421

420-
@safe_call
421-
def execute_raw(self, update_spec, multi=True, **kwargs):
422-
collection = self.get_collection()
423-
criteria = self.build_query()._mongo_query
424-
options = self.connection.operation_flags.get('update', {})
425-
options = dict(options, **kwargs)
426-
info = collection.update(criteria, update_spec, multi=multi, **options)
427-
if info is not None:
428-
return info.get('n')
429-
430-
def execute_sql(self, result_type):
431-
return self.execute_raw(*self._get_update_spec())
432-
433-
def _get_update_spec(self):
422+
def update(self, values):
434423
multi = True
435424
spec = {}
436-
for field, _, value in self.query.values:
425+
for field, value in values:
437426
if getattr(field, 'forbids_updates', False):
438427
raise DatabaseError("Updates on %ss are not allowed" %
439428
field.__class__.__name__)
440429
if field.unique:
441430
multi = False
442-
if hasattr(value, 'prepare_database_save'):
443-
value = value.prepare_database_save(field)
444-
else:
445-
value = field.get_db_prep_save(value, connection=self.connection)
446-
447-
value = self.convert_value_for_db(field.db_type(connection=self.connection), value)
448431
if hasattr(value, "evaluate"):
449432
assert value.connector in (value.ADD, value.SUB)
450433
assert not value.negated
@@ -465,7 +448,18 @@ def _get_update_spec(self):
465448
raise DatabaseError("Can not modify _id")
466449
spec.setdefault(action, {})[column] = value
467450

468-
return spec, multi
451+
return self.execute_update(spec, multi)
452+
453+
@safe_call
454+
def execute_update(self, update_spec, multi=True, **kwargs):
455+
collection = self.get_collection()
456+
criteria = self.build_query()._mongo_query
457+
options = self.connection.operation_flags.get('update', {})
458+
options = dict(options, **kwargs)
459+
info = collection.update(criteria, update_spec, multi=multi, **options)
460+
if info is not None:
461+
return info.get('n')
462+
469463

470464
class SQLDeleteCompiler(NonrelDeleteCompiler, SQLCompiler):
471465
pass

django_mongodb_engine/contrib/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def raw_update(self, spec_or_q, update_dict, **kwargs):
4646
queryset = self.filter(spec_or_q)
4747
queryset._for_write = True
4848
compiler = _compiler_for_queryset(queryset, 'SQLUpdateCompiler')
49-
compiler.execute_raw(update_dict, **kwargs)
49+
compiler.execute_update(update_dict, **kwargs)
5050

5151
raw_update.alters_data = True
5252

0 commit comments

Comments
 (0)