added meta support for indexes ensured at call-time
This commit is contained in:
parent
2e74c93878
commit
a6d64b2010
@ -154,8 +154,12 @@ class TopLevelDocumentMetaclass(DocumentMetaclass):
|
||||
'allow_inheritance': True,
|
||||
'max_documents': None,
|
||||
'max_size': None,
|
||||
'indexes': [] # indexes to be ensured at runtime
|
||||
}
|
||||
|
||||
# Apply document-defined meta options
|
||||
meta.update(attrs.get('meta', {}))
|
||||
|
||||
# Only simple classes - direct subclasses of Document - may set
|
||||
# allow_inheritance to False
|
||||
if not simple_class and not meta['allow_inheritance']:
|
||||
|
@ -30,18 +30,32 @@ class QuerySet(object):
|
||||
"""
|
||||
if isinstance(key_or_list, basestring):
|
||||
# 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:]
|
||||
self._collection.ensure_index(key_or_list)
|
||||
elif isinstance(key_or_list, (list, tuple)):
|
||||
print key_or_list
|
||||
self._collection.ensure_index(key_or_list)
|
||||
index_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
|
||||
|
||||
def __call__(self, **query):
|
||||
"""Filter the selected documents by calling the
|
||||
: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)
|
||||
self._query.update(query)
|
||||
return self
|
||||
|
Loading…
x
Reference in New Issue
Block a user