diff --git a/docs/upgrade.rst b/docs/upgrade.rst index d6eabfe6..bfb5cc51 100644 --- a/docs/upgrade.rst +++ b/docs/upgrade.rst @@ -76,7 +76,7 @@ To upgrade use a Mixin class to set meta like so :: class MyAceDocument(Document, BaseMixin): pass - MyAceDocument._get_collection_name() == myacedocument + MyAceDocument._get_collection_name() == "myacedocument" Alternatively, you can rename your collections eg :: diff --git a/mongoengine/base.py b/mongoengine/base.py index 04b3f1ff..adb40d87 100644 --- a/mongoengine/base.py +++ b/mongoengine/base.py @@ -478,13 +478,18 @@ class DocumentMetaclass(type): attrs.update(dict([(k, v) for k, v in base.__dict__.items() if issubclass(v.__class__, BaseField)])) + # Handle simple mixin's with meta + if hasattr(base, 'meta') and not isinstance(base, DocumentMetaclass): + meta = attrs.get('meta', {}) + meta.update(base.meta) + attrs['meta'] = meta + for p_base in base.__bases__: #optimize :-) if p_base in (object, BaseDocument): continue attrs.update(_get_mixin_fields(p_base)) - return attrs metaclass = attrs.get('__metaclass__') @@ -498,6 +503,7 @@ class DocumentMetaclass(type): simple_class = True for base in bases: + # Include all fields present in superclasses if hasattr(base, '_fields'): doc_fields.update(base._fields) @@ -526,7 +532,8 @@ class DocumentMetaclass(type): simple_class = False doc_class_name = '.'.join(reversed(class_name)) - meta = attrs.get('_meta', attrs.get('meta', {})) + meta = attrs.get('_meta', {}) + meta.update(attrs.get('meta', {})) if 'allow_inheritance' not in meta: meta['allow_inheritance'] = True diff --git a/tests/document.py b/tests/document.py index 910a78fc..20f3924f 100644 --- a/tests/document.py +++ b/tests/document.py @@ -96,7 +96,7 @@ class DocumentTest(unittest.TestCase): # Ensure Document isn't treated like an actual document self.assertFalse(hasattr(Document, '_fields')) - def test_collection_name(self): + def test_collection_naming(self): """Ensure that a collection with a specified name may be used. """ @@ -157,11 +157,12 @@ class DocumentTest(unittest.TestCase): } class BaseDocument(Document, BaseMixin): - pass + meta = {'allow_inheritance': True} class MyDocument(BaseDocument): pass - self.assertEquals('mydocument', OldMixinNamingConvention._get_collection_name()) + + self.assertEquals('basedocument', MyDocument._get_collection_name()) def test_get_superclasses(self): """Ensure that the correct list of superclasses is assembled.