Merge branch 'master' of git://github.com/punteney/mongoengine

This commit is contained in:
Harry Marr
2010-01-31 13:11:20 +00:00
2 changed files with 42 additions and 2 deletions

View File

@@ -14,10 +14,14 @@ REPR_OUTPUT_SIZE = 20
class InvalidQueryError(Exception):
pass
class OperationError(Exception):
pass
class MultipleObjectsReturned(Exception):
pass
class DoesNotExist(Exception):
pass
class Q(object):
@@ -292,6 +296,21 @@ class QuerySet(object):
return mongo_query
def get(self, *q_objs, **query):
"""Retrieve the the matching object raising
'MultipleObjectsReturned' or 'DoesNotExist' exceptions
if multiple or no results are found.
"""
self.__call__(*q_objs, **query)
count = self.count()
if count == 1:
return self[0]
elif count > 1:
raise MultipleObjectsReturned
else:
raise DoesNotExist
def first(self):
"""Retrieve the first object matching the query.
"""