Added document mixin support
For extendable / reusable documents Fixes #204
This commit is contained in:
parent
e04e5f42ef
commit
1b0323bc22
@ -5,6 +5,7 @@ Changelog
|
|||||||
Changes in dev
|
Changes in dev
|
||||||
==============
|
==============
|
||||||
|
|
||||||
|
- Added Document Mixin support
|
||||||
- Fixed queryet __repr__ mid iteration
|
- Fixed queryet __repr__ mid iteration
|
||||||
- Added hint() support, so cantell Mongo the proper index to use for the query
|
- Added hint() support, so cantell Mongo the proper index to use for the query
|
||||||
- Fixed issue with inconsitent setting of _cls breaking inherited referencing
|
- Fixed issue with inconsitent setting of _cls breaking inherited referencing
|
||||||
|
@ -389,6 +389,7 @@ class DocumentMetaclass(type):
|
|||||||
class_name = [name]
|
class_name = [name]
|
||||||
superclasses = {}
|
superclasses = {}
|
||||||
simple_class = True
|
simple_class = True
|
||||||
|
|
||||||
for base in bases:
|
for base in bases:
|
||||||
# Include all fields present in superclasses
|
# Include all fields present in superclasses
|
||||||
if hasattr(base, '_fields'):
|
if hasattr(base, '_fields'):
|
||||||
@ -397,6 +398,9 @@ class DocumentMetaclass(type):
|
|||||||
# Get superclasses from superclass
|
# Get superclasses from superclass
|
||||||
superclasses[base._class_name] = base
|
superclasses[base._class_name] = base
|
||||||
superclasses.update(base._superclasses)
|
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'):
|
if hasattr(base, '_meta') and not base._meta.get('abstract'):
|
||||||
# Ensure that the Document class may be subclassed -
|
# Ensure that the Document class may be subclassed -
|
||||||
|
@ -1380,6 +1380,36 @@ class DocumentTest(unittest.TestCase):
|
|||||||
promoted_employee.reload()
|
promoted_employee.reload()
|
||||||
self.assertEqual(promoted_employee.details, None)
|
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):
|
def test_save_reference(self):
|
||||||
"""Ensure that a document reference field may be saved in the database.
|
"""Ensure that a document reference field may be saved in the database.
|
||||||
|
@ -18,8 +18,7 @@ class PickleTest(Document):
|
|||||||
|
|
||||||
|
|
||||||
class Mixin(object):
|
class Mixin(object):
|
||||||
number = IntField()
|
name = StringField()
|
||||||
string = StringField(choices=(('One', '1'), ('Two', '2')))
|
|
||||||
|
|
||||||
|
|
||||||
class Base(Document):
|
class Base(Document):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user