Fixes collection creation post drop_collection

Thanks to Julien Rebetez for the original patch
closes [#285]
This commit is contained in:
Ross Lawley
2011-10-11 02:26:33 -07:00
parent 5fb9d61d28
commit 0624cdd6e4
5 changed files with 38 additions and 11 deletions

View File

@@ -36,7 +36,6 @@ class EmbeddedDocument(BaseDocument):
super(EmbeddedDocument, self).__delattr__(*args, **kwargs)
class Document(BaseDocument):
"""The base class used for defining the structure and properties of
collections of documents stored in MongoDB. Inherit from this class, and
@@ -81,7 +80,6 @@ class Document(BaseDocument):
@classmethod
def _get_collection(self):
"""Returns the collection for the document."""
if not hasattr(self, '_collection') or self._collection is None:
db = _get_db()
collection_name = self._get_collection_name()
@@ -291,8 +289,10 @@ class Document(BaseDocument):
"""Drops the entire collection associated with this
:class:`~mongoengine.Document` type from the database.
"""
from mongoengine.queryset import QuerySet
db = _get_db()
db.drop_collection(cls._get_collection_name())
QuerySet._reset_already_indexed(cls)
class DynamicDocument(Document):

View File

@@ -434,9 +434,11 @@ class QuerySet(object):
return spec
@classmethod
def _reset_already_indexed(cls):
def _reset_already_indexed(cls, document=None):
"""Helper to reset already indexed, can be useful for testing purposes"""
cls.__already_indexed = set()
if document:
cls.__already_indexed.discard(document)
cls.__already_indexed.clear()
def __call__(self, q_obj=None, class_check=True, slave_okay=False, **query):
"""Filter the selected documents by calling the
@@ -476,6 +478,13 @@ class QuerySet(object):
perform operations only if the collection is accessed.
"""
if self._document not in QuerySet.__already_indexed:
# Ensure collection exists
db = _get_db()
if self._collection_obj.name not in db.collection_names():
self._document._collection = None
self._collection_obj = self._document._get_collection()
QuerySet.__already_indexed.add(self._document)
background = self._document._meta.get('index_background', False)