Index specs now use proper field names
This commit is contained in:
parent
960aea2fd4
commit
eb3e6963fa
@ -30,18 +30,23 @@ 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("-") or key_or_list.startswith("+"):
|
if 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)
|
# Use real field name
|
||||||
|
key = QuerySet._translate_field_name(self._document, key_or_list)
|
||||||
|
self._collection.ensure_index(key)
|
||||||
elif isinstance(key_or_list, (list, tuple)):
|
elif isinstance(key_or_list, (list, tuple)):
|
||||||
index_list = []
|
index_list = []
|
||||||
for key in key_or_list:
|
for key in key_or_list:
|
||||||
|
# Get direction from + or -
|
||||||
|
direction = pymongo.ASCENDING
|
||||||
if key.startswith("-"):
|
if key.startswith("-"):
|
||||||
index_list.append((key[1:], pymongo.DESCENDING))
|
direction = pymongo.DESCENDING
|
||||||
else:
|
if key.startswith(("+", "-")):
|
||||||
if key.startswith("+"):
|
|
||||||
key = key[1:]
|
key = key[1:]
|
||||||
index_list.append((key, pymongo.ASCENDING))
|
# Use real field name
|
||||||
|
key = QuerySet._translate_field_name(self._document, key)
|
||||||
|
index_list.append((key, direction))
|
||||||
self._collection.ensure_index(index_list)
|
self._collection.ensure_index(index_list)
|
||||||
return self
|
return self
|
||||||
|
|
||||||
@ -89,10 +94,12 @@ class QuerySet(object):
|
|||||||
return fields
|
return fields
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def _translate_field_name(cls, doc_cls, parts):
|
def _translate_field_name(cls, doc_cls, field, sep='.'):
|
||||||
"""Translate a field attribute name to a database field name.
|
"""Translate a field attribute name to a database field name.
|
||||||
"""
|
"""
|
||||||
return [field.name for field in QuerySet._lookup_field(doc_cls, parts)]
|
parts = field.split(sep)
|
||||||
|
parts = [f.name for f in QuerySet._lookup_field(doc_cls, parts)]
|
||||||
|
return '.'.join(parts)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def _transform_query(cls, _doc_cls=None, **query):
|
def _transform_query(cls, _doc_cls=None, **query):
|
||||||
|
@ -225,7 +225,7 @@ class DocumentTest(unittest.TestCase):
|
|||||||
"""Ensure that indexes are used when meta[indexes] is specified.
|
"""Ensure that indexes are used when meta[indexes] is specified.
|
||||||
"""
|
"""
|
||||||
class BlogPost(Document):
|
class BlogPost(Document):
|
||||||
date = DateTimeField(default=datetime.datetime.now)
|
date = DateTimeField(name='addDate', default=datetime.datetime.now)
|
||||||
category = StringField()
|
category = StringField()
|
||||||
meta = {
|
meta = {
|
||||||
'indexes': [
|
'indexes': [
|
||||||
@ -241,9 +241,9 @@ class DocumentTest(unittest.TestCase):
|
|||||||
|
|
||||||
BlogPost.objects()
|
BlogPost.objects()
|
||||||
info = BlogPost.objects._collection.index_information()
|
info = BlogPost.objects._collection.index_information()
|
||||||
self.assertTrue([('category', 1), ('date', -1)] in info.values())
|
self.assertTrue([('category', 1), ('addDate', -1)] in info.values())
|
||||||
# Even though descending order was specified, single-key indexes use 1
|
# Even though descending order was specified, single-key indexes use 1
|
||||||
self.assertTrue([('date', 1)] in info.values())
|
self.assertTrue([('addDate', 1)] in info.values())
|
||||||
|
|
||||||
BlogPost.drop_collection()
|
BlogPost.drop_collection()
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user