Added basic querying - find and find_one
This commit is contained in:
@@ -44,7 +44,7 @@ class BaseField(object):
|
||||
try:
|
||||
value = self._to_python(value)
|
||||
self._validate(value)
|
||||
except ValueError:
|
||||
except (ValueError, AttributeError):
|
||||
raise ValidationError('Invalid value for field of type "' +
|
||||
self.__class__.__name__ + '"')
|
||||
elif self.required:
|
||||
@@ -145,7 +145,7 @@ class TopLevelDocumentMetaclass(DocumentMetaclass):
|
||||
# Set up collection manager, needs the class to have fields so use
|
||||
# DocumentMetaclass before instantiating CollectionManager object
|
||||
new_class = super_new(cls, name, bases, attrs)
|
||||
setattr(new_class, 'collection', CollectionManager(new_class))
|
||||
new_class.objects = CollectionManager(new_class)
|
||||
|
||||
return new_class
|
||||
|
||||
@@ -204,3 +204,11 @@ class BaseDocument(object):
|
||||
if value is not None:
|
||||
data[field_name] = field._to_mongo(value)
|
||||
return data
|
||||
|
||||
@classmethod
|
||||
def _from_son(cls, son):
|
||||
"""Create an instance of a Document (subclass) from a PyMongo SOM.
|
||||
"""
|
||||
data = dict((str(key), value) for key, value in son.items())
|
||||
return cls(**data)
|
||||
|
||||
|
||||
@@ -1,5 +1,29 @@
|
||||
from connection import _get_db
|
||||
|
||||
|
||||
class QuerySet(object):
|
||||
"""A set of results returned from a query. Wraps a MongoDB cursor,
|
||||
providing Document objects as the results.
|
||||
"""
|
||||
|
||||
def __init__(self, document, cursor):
|
||||
self._document = document
|
||||
self._cursor = cursor
|
||||
|
||||
def next(self):
|
||||
"""Wrap the result in a Document object.
|
||||
"""
|
||||
return self._document._from_son(self._cursor.next())
|
||||
|
||||
def count(self):
|
||||
"""Count the selected elements in the query.
|
||||
"""
|
||||
return self._cursor.count()
|
||||
|
||||
def __iter__(self):
|
||||
return self
|
||||
|
||||
|
||||
class CollectionManager(object):
|
||||
|
||||
def __init__(self, document):
|
||||
@@ -14,4 +38,15 @@ class CollectionManager(object):
|
||||
def _save_document(self, document):
|
||||
"""Save the provided document to the collection.
|
||||
"""
|
||||
_id = self._collection.save(document)
|
||||
_id = self._collection.save(document._to_mongo())
|
||||
document._id = _id
|
||||
|
||||
def find(self, query=None):
|
||||
"""Query the collection for document matching the provided query.
|
||||
"""
|
||||
return QuerySet(self._document, self._collection.find(query))
|
||||
|
||||
def find_one(self, query=None):
|
||||
"""Query the collection for document matching the provided query.
|
||||
"""
|
||||
return self._document._from_son(self._collection.find_one(query))
|
||||
|
||||
@@ -14,4 +14,7 @@ class Document(BaseDocument):
|
||||
__metaclass__ = TopLevelDocumentMetaclass
|
||||
|
||||
def save(self):
|
||||
self.collection._save_document(self._to_mongo())
|
||||
"""Save the document to the database. If the document already exists,
|
||||
it will be updated, otherwise it will be created.
|
||||
"""
|
||||
self.objects._save_document(self)
|
||||
|
||||
@@ -59,6 +59,8 @@ class EmbeddedDocumentField(BaseField):
|
||||
super(EmbeddedDocumentField, self).__init__(**kwargs)
|
||||
|
||||
def _to_python(self, value):
|
||||
if not isinstance(value, self.document):
|
||||
return self.document._from_son(value)
|
||||
return value
|
||||
|
||||
def _to_mongo(self, value):
|
||||
@@ -68,6 +70,7 @@ class EmbeddedDocumentField(BaseField):
|
||||
"""Make sure that the document instance is an instance of the
|
||||
EmbeddedDocument subclass provided when the document was defined.
|
||||
"""
|
||||
# Using isinstance also works for subclasses of self.document
|
||||
if not isinstance(value, self.document):
|
||||
raise ValidationError('Invalid embedded document instance '
|
||||
'provided to an EmbeddedDocumentField')
|
||||
|
||||
Reference in New Issue
Block a user