Index specs now use proper field names
This commit is contained in:
		@@ -30,18 +30,23 @@ class QuerySet(object):
 | 
			
		||||
        """
 | 
			
		||||
        if isinstance(key_or_list, basestring):
 | 
			
		||||
            # 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:]
 | 
			
		||||
            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)):
 | 
			
		||||
            index_list = []
 | 
			
		||||
            for key in key_or_list:
 | 
			
		||||
                # Get direction from + or -
 | 
			
		||||
                direction = pymongo.ASCENDING
 | 
			
		||||
                if key.startswith("-"):
 | 
			
		||||
                    index_list.append((key[1:], pymongo.DESCENDING))
 | 
			
		||||
                else:
 | 
			
		||||
                    if key.startswith("+"):
 | 
			
		||||
                    direction = pymongo.DESCENDING
 | 
			
		||||
                if key.startswith(("+", "-")):
 | 
			
		||||
                        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)
 | 
			
		||||
        return self
 | 
			
		||||
 | 
			
		||||
@@ -89,10 +94,12 @@ class QuerySet(object):
 | 
			
		||||
        return fields
 | 
			
		||||
 | 
			
		||||
    @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.
 | 
			
		||||
        """
 | 
			
		||||
        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
 | 
			
		||||
    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.
 | 
			
		||||
        """
 | 
			
		||||
        class BlogPost(Document):
 | 
			
		||||
            date = DateTimeField(default=datetime.datetime.now)
 | 
			
		||||
            date = DateTimeField(name='addDate', default=datetime.datetime.now)
 | 
			
		||||
            category = StringField()
 | 
			
		||||
            meta = {
 | 
			
		||||
                'indexes': [
 | 
			
		||||
@@ -241,9 +241,9 @@ class DocumentTest(unittest.TestCase):
 | 
			
		||||
 | 
			
		||||
        BlogPost.objects()
 | 
			
		||||
        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
 | 
			
		||||
        self.assertTrue([('date', 1)] in info.values())
 | 
			
		||||
        self.assertTrue([('addDate', 1)] in info.values())
 | 
			
		||||
 | 
			
		||||
        BlogPost.drop_collection()
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user