added get-method to fetch exactly one document from the collection. catching pymongo's ObjectId-errors and raising mongoengine's ValidationError instead.

This commit is contained in:
Florian Schlachter
2010-01-31 01:11:37 +01:00
parent b3cc2f990a
commit 7d6e117f68
2 changed files with 19 additions and 3 deletions

View File

@@ -6,7 +6,6 @@ import pymongo
class ValidationError(Exception):
pass
class BaseField(object):
"""A base class for fields in a MongoDB document. Instances of this class
may be added to subclasses of `Document` to define a document's schema.
@@ -76,7 +75,10 @@ class ObjectIdField(BaseField):
def to_mongo(self, value):
if not isinstance(value, pymongo.objectid.ObjectId):
return pymongo.objectid.ObjectId(str(value))
try:
return pymongo.objectid.ObjectId(str(value))
except Exception, e:
raise ValidationError(e.message)
return value
def prepare_query_value(self, value):