only create indexes on first collection access (fix #223)

This commit is contained in:
Dan Crosta 2011-07-11 09:44:28 -04:00
parent 147e33c3ca
commit 859de712b4

View File

@ -320,10 +320,11 @@ class QuerySet(object):
providing :class:`~mongoengine.Document` objects as the results. providing :class:`~mongoengine.Document` objects as the results.
""" """
ALREADY_INDEXED = set()
def __init__(self, document, collection): def __init__(self, document, collection):
self._document = document self._document = document
self._collection_obj = collection self._collection_obj = collection
self._accessed_collection = False
self._mongo_query = None self._mongo_query = None
self._query_obj = Q() self._query_obj = Q()
self._initial_query = {} self._initial_query = {}
@ -467,8 +468,8 @@ class QuerySet(object):
"""Property that returns the collection object. This allows us to """Property that returns the collection object. This allows us to
perform operations only if the collection is accessed. perform operations only if the collection is accessed.
""" """
if not self._accessed_collection: if self._document not in QuerySet.ALREADY_INDEXED:
self._accessed_collection = True QuerySet.ALREADY_INDEXED.add(self._document)
background = self._document._meta.get('index_background', False) background = self._document._meta.get('index_background', False)
drop_dups = self._document._meta.get('index_drop_dups', False) drop_dups = self._document._meta.get('index_drop_dups', False)