Merge branch 'v0.4' of git://github.com/hmarr/mongoengine into v0.4

This commit is contained in:
Steve Challis
2010-09-29 23:39:09 +01:00
8 changed files with 144 additions and 26 deletions

View File

@@ -344,6 +344,8 @@ class QuerySet(object):
mongo_query = {}
for key, value in query.items():
parts = key.split('__')
indices = [(i, p) for i, p in enumerate(parts) if p.isdigit()]
parts = [part for part in parts if not part.isdigit()]
# Check for an operator and transform to mongo-style if there is
op = None
if parts[-1] in operators + match_operators + geo_operators:
@@ -381,7 +383,9 @@ class QuerySet(object):
"been implemented" % op)
elif op not in match_operators:
value = {'$' + op: value}
for i, part in indices:
parts.insert(i, part)
key = '.'.join(parts)
if op is None or key not in mongo_query:
mongo_query[key] = value
@@ -762,7 +766,8 @@ class QuerySet(object):
return mongo_update
def update(self, safe_update=True, upsert=False, **update):
"""Perform an atomic update on the fields matched by the query.
"""Perform an atomic update on the fields matched by the query. When
``safe_update`` is used, the number of affected documents is returned.
:param safe: check if the operation succeeded before returning
:param update: Django-style update keyword arguments
@@ -774,8 +779,10 @@ class QuerySet(object):
update = QuerySet._transform_update(self._document, **update)
try:
self._collection.update(self._query, update, safe=safe_update,
upsert=upsert, multi=True)
ret = self._collection.update(self._query, update, multi=True,
upsert=upsert, safe=safe_update)
if ret is not None and 'n' in ret:
return ret['n']
except pymongo.errors.OperationFailure, err:
if unicode(err) == u'multi not coded yet':
message = u'update() method requires MongoDB 1.1.3+'
@@ -783,7 +790,8 @@ class QuerySet(object):
raise OperationError(u'Update failed (%s)' % unicode(err))
def update_one(self, safe_update=True, upsert=False, **update):
"""Perform an atomic update on first field matched by the query.
"""Perform an atomic update on first field matched by the query. When
``safe_update`` is used, the number of affected documents is returned.
:param safe: check if the operation succeeded before returning
:param update: Django-style update keyword arguments
@@ -795,11 +803,14 @@ class QuerySet(object):
# Explicitly provide 'multi=False' to newer versions of PyMongo
# as the default may change to 'True'
if pymongo.version >= '1.1.1':
self._collection.update(self._query, update, safe=safe_update,
upsert=upsert, multi=False)
ret = self._collection.update(self._query, update, multi=False,
upsert=upsert, safe=safe_update)
else:
# Older versions of PyMongo don't support 'multi'
self._collection.update(self._query, update, safe=safe_update)
ret = self._collection.update(self._query, update,
safe=safe_update)
if ret is not None and 'n' in ret:
return ret['n']
except pymongo.errors.OperationFailure, e:
raise OperationError(u'Update failed [%s]' % unicode(e))
@@ -988,7 +999,8 @@ class QuerySetManager(object):
self._collection = db[collection]
# owner is the document that contains the QuerySetManager
queryset = QuerySet(owner, self._collection)
queryset_class = owner._meta['queryset_class'] or QuerySet
queryset = queryset_class(owner, self._collection)
if self._manager_func:
if self._manager_func.func_code.co_argcount == 1:
queryset = self._manager_func(queryset)