Skip to content

Commit df4003d

Browse files
committed
fix QuerySet.update() with unique fields
fixes #14
1 parent 28f52b1 commit df4003d

File tree

1 file changed

+3
-9
lines changed

1 file changed

+3
-9
lines changed

django_mongodb/compiler.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,6 @@ def execute_sql(self, result_type):
244244
return self.update(values)
245245

246246
def update(self, values):
247-
multi = True
248247
spec = {}
249248
for field, value in values:
250249
if field.primary_key:
@@ -253,23 +252,18 @@ def update(self, values):
253252
raise NotSupportedError("QuerySet.update() with expression not supported.")
254253
# .update(foo=123) --> {'$set': {'foo': 123}}
255254
spec.setdefault("$set", {})[field.column] = value
256-
257-
if field.unique:
258-
multi = False
259-
260-
return self.execute_update(spec, multi)
255+
return self.execute_update(spec)
261256

262257
@wrap_database_errors
263-
def execute_update(self, update_spec, multi=True, **kwargs):
258+
def execute_update(self, update_spec, **kwargs):
264259
collection = self.get_collection()
265260
try:
266261
criteria = self.build_query().mongo_query
267262
except EmptyResultSet:
268263
return 0
269264
options = self.connection.operation_flags.get("update", {})
270265
options = dict(options, **kwargs)
271-
method = "update_many" if multi else "update_one"
272-
return getattr(collection, method)(criteria, update_spec, **options).matched_count
266+
return collection.update_many(criteria, update_spec, **options).matched_count
273267

274268

275269
class SQLAggregateCompiler(SQLCompiler):

0 commit comments

Comments
 (0)