added 'upsert' arg to QuerySet.update and QuerySet.update_one

This commit is contained in:
blackbrrr 2010-03-08 21:59:54 -06:00
parent 3b6d8fab47
commit 26c6e4997c

View File

@ -588,7 +588,7 @@ class QuerySet(object):
return mongo_update
def update(self, safe_update=True, **update):
def update(self, safe_update=True, upsert=False, **update):
"""Perform an atomic update on the fields matched by the query.
:param safe: check if the operation succeeded before returning
@ -602,14 +602,14 @@ class QuerySet(object):
update = QuerySet._transform_update(self._document, **update)
try:
self._collection.update(self._query, update, safe=safe_update,
multi=True)
upsert=upsert, multi=True)
except pymongo.errors.OperationFailure, err:
if unicode(err) == u'multi not coded yet':
message = u'update() method requires MongoDB 1.1.3+'
raise OperationError(message)
raise OperationError(u'Update failed (%s)' % unicode(err))
def update_one(self, safe_update=True, **update):
def update_one(self, safe_update=True, upsert=False, **update):
"""Perform an atomic update on first field matched by the query.
:param safe: check if the operation succeeded before returning
@ -623,7 +623,7 @@ class QuerySet(object):
# as the default may change to 'True'
if pymongo.version >= '1.1.1':
self._collection.update(self._query, update, safe=safe_update,
multi=False)
upsert=upsert, multi=False)
else:
# Older versions of PyMongo don't support 'multi'
self._collection.update(self._query, update, safe=safe_update)