Allow for types to never be auto-prepended to indices

This commit is contained in:
Colin Howe 2011-06-08 12:20:58 +01:00
parent c903af032f
commit 7c62fdc0b8
2 changed files with 22 additions and 3 deletions

View File

@ -410,8 +410,10 @@ class QuerySet(object):
if use_types and not all(f._index_with_types for f in fields): if use_types and not all(f._index_with_types for f in fields):
use_types = False use_types = False
# If _types is being used, prepend it to every specified index # If _types is being used, create an index for it
if doc_cls._meta.get('allow_inheritance') and use_types: index_types = doc_cls._meta.get('index_types', True)
allow_inheritance = doc_cls._meta.get('allow_inheritance')
if index_types and allow_inheritance and use_types:
index_list.insert(0, ('_types', 1)) index_list.insert(0, ('_types', 1))
return index_list return index_list
@ -457,6 +459,7 @@ class QuerySet(object):
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)
index_opts = self._document._meta.get('index_options', {}) index_opts = self._document._meta.get('index_options', {})
index_types = self._document._meta.get('index_types', True)
# Ensure indexes created by uniqueness constraints # Ensure indexes created by uniqueness constraints
for index in self._document._meta['unique_indexes']: for index in self._document._meta['unique_indexes']:
@ -470,7 +473,7 @@ class QuerySet(object):
background=background, **index_opts) background=background, **index_opts)
# If _types is being used (for polymorphism), it needs an index # If _types is being used (for polymorphism), it needs an index
if '_types' in self._query: if index_types and '_types' in self._query:
self._collection.ensure_index('_types', self._collection.ensure_index('_types',
background=background, **index_opts) background=background, **index_opts)

View File

@ -1710,6 +1710,22 @@ class QuerySetTest(unittest.TestCase):
self.assertTrue([('_types', 1)] in info) self.assertTrue([('_types', 1)] in info)
self.assertTrue([('_types', 1), ('date', -1)] in info) self.assertTrue([('_types', 1), ('date', -1)] in info)
def test_dont_index_types(self):
"""Ensure that index_types will, when disabled, prevent _types
being added to all indices.
"""
class BlogPost(Document):
date = DateTimeField()
meta = {'index_types': False,
'indexes': ['-date']}
# Indexes are lazy so use list() to perform query
list(BlogPost.objects)
info = BlogPost.objects._collection.index_information()
info = [value['key'] for key, value in info.iteritems()]
self.assertTrue([('_types', 1)] not in info)
self.assertTrue([('date', -1)] in info)
BlogPost.drop_collection() BlogPost.drop_collection()
class BlogPost(Document): class BlogPost(Document):