diff --git a/docs/changelog.rst b/docs/changelog.rst index 48b58483..e3cd7232 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -5,6 +5,7 @@ Changelog Changes in dev ============== +- Added Document Mixin support - Fixed queryet __repr__ mid iteration - Added hint() support, so cantell Mongo the proper index to use for the query - Fixed issue with inconsitent setting of _cls breaking inherited referencing diff --git a/mongoengine/base.py b/mongoengine/base.py index 938808a8..f8d415b0 100644 --- a/mongoengine/base.py +++ b/mongoengine/base.py @@ -389,6 +389,7 @@ class DocumentMetaclass(type): class_name = [name] superclasses = {} simple_class = True + for base in bases: # Include all fields present in superclasses if hasattr(base, '_fields'): @@ -397,6 +398,9 @@ class DocumentMetaclass(type): # Get superclasses from superclass superclasses[base._class_name] = base superclasses.update(base._superclasses) + else: # Add any mixin fields + attrs.update(dict([(k,v) for k,v in base.__dict__.items() + if issubclass(v.__class__, BaseField)])) if hasattr(base, '_meta') and not base._meta.get('abstract'): # Ensure that the Document class may be subclassed - diff --git a/tests/document.py b/tests/document.py index 8f9364fe..c5aa6e89 100644 --- a/tests/document.py +++ b/tests/document.py @@ -1380,6 +1380,36 @@ class DocumentTest(unittest.TestCase): promoted_employee.reload() self.assertEqual(promoted_employee.details, None) + def test_mixins_dont_add_to_types(self): + + class Bob(Document): name = StringField() + + Bob.drop_collection() + + p = Bob(name="Rozza") + p.save() + Bob.drop_collection() + + class Person(Document, Mixin): + pass + + Person.drop_collection() + + p = Person(name="Rozza") + p.save() + self.assertEquals(p._fields.keys(), ['name', 'id']) + + collection = self.db[Person._meta['collection']] + obj = collection.find_one() + self.assertEquals(obj['_cls'], 'Person') + self.assertEquals(obj['_types'], ['Person']) + + + + self.assertEquals(Person.objects.count(), 1) + rozza = Person.objects.get(name="Rozza") + + Person.drop_collection() def test_save_reference(self): """Ensure that a document reference field may be saved in the database. diff --git a/tests/fixtures.py b/tests/fixtures.py index 483b7184..5aaba556 100644 --- a/tests/fixtures.py +++ b/tests/fixtures.py @@ -18,8 +18,7 @@ class PickleTest(Document): class Mixin(object): - number = IntField() - string = StringField(choices=(('One', '1'), ('Two', '2'))) + name = StringField() class Base(Document):