added meta support for indexes ensured at call-time

This commit is contained in:
blackbrrr 2010-01-06 04:28:24 +08:00 committed by Harry Marr
parent 2e74c93878
commit a6d64b2010
2 changed files with 21 additions and 3 deletions

View File

@ -154,8 +154,12 @@ class TopLevelDocumentMetaclass(DocumentMetaclass):
'allow_inheritance': True, 'allow_inheritance': True,
'max_documents': None, 'max_documents': None,
'max_size': None, 'max_size': None,
'indexes': [] # indexes to be ensured at runtime
} }
# Apply document-defined meta options
meta.update(attrs.get('meta', {})) meta.update(attrs.get('meta', {}))
# Only simple classes - direct subclasses of Document - may set # Only simple classes - direct subclasses of Document - may set
# allow_inheritance to False # allow_inheritance to False
if not simple_class and not meta['allow_inheritance']: if not simple_class and not meta['allow_inheritance']:

View File

@ -30,18 +30,32 @@ class QuerySet(object):
""" """
if isinstance(key_or_list, basestring): if isinstance(key_or_list, basestring):
# single-field indexes needn't specify a direction # single-field indexes needn't specify a direction
if key_or_list.startswith("-"): if key_or_list.startswith("-") or key_or_list.startswith("+"):
key_or_list = key_or_list[1:] key_or_list = key_or_list[1:]
self._collection.ensure_index(key_or_list) self._collection.ensure_index(key_or_list)
elif isinstance(key_or_list, (list, tuple)): elif isinstance(key_or_list, (list, tuple)):
print key_or_list index_list = []
self._collection.ensure_index(key_or_list) for key in key_or_list:
if key.startswith("-"):
index_list.append((key[1:], pymongo.DESCENDING))
else:
if key.startswith("+"):
key = key[1:]
index_list.append((key, pymongo.ASCENDING))
self._collection.ensure_index(index_list)
return self return self
def __call__(self, **query): def __call__(self, **query):
"""Filter the selected documents by calling the """Filter the selected documents by calling the
:class:`~mongoengine.QuerySet` with a query. :class:`~mongoengine.QuerySet` with a query.
""" """
# ensure document-defined indexes are created
if self._document._meta['indexes']:
for key_or_list in self._document._meta['indexes']:
# print "key", key_or_list
self.ensure_index(key_or_list)
query = QuerySet._transform_query(_doc_cls=self._document, **query) query = QuerySet._transform_query(_doc_cls=self._document, **query)
self._query.update(query) self._query.update(query)
return self return self