Fixed indexing - turn off _cls (#414)

This commit is contained in:
Ross Lawley 2013-07-30 11:43:52 +00:00
parent dc310b99f9
commit 0c43787996
5 changed files with 57 additions and 2 deletions

View File

@ -4,6 +4,7 @@ Changelog
Changes in 0.8.4 Changes in 0.8.4
================ ================
- Fixed indexing - turn off _cls (#414)
- Fixed dereference threading issue in ComplexField.__get__ (#412) - Fixed dereference threading issue in ComplexField.__get__ (#412)
- Fixed QuerySetNoCache.count() caching (#410) - Fixed QuerySetNoCache.count() caching (#410)
- Don't follow references in _get_changed_fields (#422, #417) - Don't follow references in _get_changed_fields (#422, #417)

View File

@ -442,6 +442,8 @@ The following example shows a :class:`Log` document that will be limited to
ip_address = StringField() ip_address = StringField()
meta = {'max_documents': 1000, 'max_size': 2000000} meta = {'max_documents': 1000, 'max_size': 2000000}
.. defining-indexes_
Indexes Indexes
======= =======
@ -485,6 +487,35 @@ If a dictionary is passed then the following options are available:
Inheritance adds extra fields indices see: :ref:`document-inheritance`. Inheritance adds extra fields indices see: :ref:`document-inheritance`.
Global index default options
----------------------------
There are a few top level defaults for all indexes that can be set::
class Page(Document):
title = StringField()
rating = StringField()
meta = {
'index_options': {},
'index_background': True,
'index_drop_dups': True,
'index_cls': False
}
:attr:`index_options` (Optional)
Set any default index options - see the `full options list <http://docs.mongodb.org/manual/reference/method/db.collection.ensureIndex/#db.collection.ensureIndex>`_
:attr:`index_background` (Optional)
Set the default value for if an index should be indexed in the background
:attr:`index_drop_dups` (Optional)
Set the default value for if an index should drop duplicates
:attr:`index_cls` (Optional)
A way to turn off a specific index for _cls.
Compound Indexes and Indexing sub documents Compound Indexes and Indexing sub documents
------------------------------------------- -------------------------------------------

View File

@ -629,8 +629,10 @@ class BaseDocument(object):
# Check to see if we need to include _cls # Check to see if we need to include _cls
allow_inheritance = cls._meta.get('allow_inheritance', allow_inheritance = cls._meta.get('allow_inheritance',
ALLOW_INHERITANCE) ALLOW_INHERITANCE)
include_cls = allow_inheritance and not spec.get('sparse', False) include_cls = (allow_inheritance and not spec.get('sparse', False) and
spec.get('cls', True))
if "cls" in spec:
spec.pop('cls')
for key in spec['fields']: for key in spec['fields']:
# If inherited spec continue # If inherited spec continue
if isinstance(key, (list, tuple)): if isinstance(key, (list, tuple)):

View File

@ -536,6 +536,8 @@ class Document(BaseDocument):
def ensure_indexes(cls): def ensure_indexes(cls):
"""Checks the document meta data and ensures all the indexes exist. """Checks the document meta data and ensures all the indexes exist.
Global defaults can be set in the meta - see :doc:`guide/defining-documents`
.. note:: You can disable automatic index creation by setting .. note:: You can disable automatic index creation by setting
`auto_create_index` to False in the documents meta data `auto_create_index` to False in the documents meta data
""" """

View File

@ -156,6 +156,25 @@ class IndexesTest(unittest.TestCase):
self.assertEqual([{'fields': [('_cls', 1), ('title', 1)]}], self.assertEqual([{'fields': [('_cls', 1), ('title', 1)]}],
A._meta['index_specs']) A._meta['index_specs'])
def test_index_no_cls(self):
"""Ensure index specs are inhertited correctly"""
class A(Document):
title = StringField()
meta = {
'indexes': [
{'fields': ('title',), 'cls': False},
],
'allow_inheritance': True,
'index_cls': False
}
self.assertEqual([('title', 1)], A._meta['index_specs'][0]['fields'])
A._get_collection().drop_indexes()
A.ensure_indexes()
info = A._get_collection().index_information()
self.assertEqual(len(info.keys()), 2)
def test_build_index_spec_is_not_destructive(self): def test_build_index_spec_is_not_destructive(self):
class MyDoc(Document): class MyDoc(Document):