Reflect Inheritance in Field's 'owner_document'
The 'owner_document' property of a Field now reflects the parent field which first contained the Field when a Document in inherited. Fixes #954 Closes #955
This commit is contained in:
parent
319f1deceb
commit
5d6a28954b
@ -83,6 +83,7 @@ class BaseField(object):
|
|||||||
self.help_text = help_text
|
self.help_text = help_text
|
||||||
self.null = null
|
self.null = null
|
||||||
self.sparse = sparse
|
self.sparse = sparse
|
||||||
|
self._owner_document = None
|
||||||
|
|
||||||
# Adjust the appropriate creation counter, and save our local copy.
|
# Adjust the appropriate creation counter, and save our local copy.
|
||||||
if self.db_field == '_id':
|
if self.db_field == '_id':
|
||||||
@ -189,6 +190,17 @@ class BaseField(object):
|
|||||||
|
|
||||||
self.validate(value, **kwargs)
|
self.validate(value, **kwargs)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def owner_document(self):
|
||||||
|
return self._owner_document
|
||||||
|
|
||||||
|
def _set_owner_document(self, owner_document):
|
||||||
|
self._owner_document = owner_document
|
||||||
|
|
||||||
|
@owner_document.setter
|
||||||
|
def owner_document(self, owner_document):
|
||||||
|
self._set_owner_document(owner_document)
|
||||||
|
|
||||||
|
|
||||||
class ComplexBaseField(BaseField):
|
class ComplexBaseField(BaseField):
|
||||||
|
|
||||||
@ -398,11 +410,6 @@ class ComplexBaseField(BaseField):
|
|||||||
self.field.owner_document = owner_document
|
self.field.owner_document = owner_document
|
||||||
self._owner_document = owner_document
|
self._owner_document = owner_document
|
||||||
|
|
||||||
def _get_owner_document(self, owner_document):
|
|
||||||
self._owner_document = owner_document
|
|
||||||
|
|
||||||
owner_document = property(_get_owner_document, _set_owner_document)
|
|
||||||
|
|
||||||
|
|
||||||
class ObjectIdField(BaseField):
|
class ObjectIdField(BaseField):
|
||||||
|
|
||||||
|
@ -176,7 +176,8 @@ class DocumentMetaclass(type):
|
|||||||
# Handle delete rules
|
# Handle delete rules
|
||||||
for field in new_class._fields.itervalues():
|
for field in new_class._fields.itervalues():
|
||||||
f = field
|
f = field
|
||||||
f.owner_document = new_class
|
if f.owner_document is None:
|
||||||
|
f.owner_document = new_class
|
||||||
delete_rule = getattr(f, 'reverse_delete_rule', DO_NOTHING)
|
delete_rule = getattr(f, 'reverse_delete_rule', DO_NOTHING)
|
||||||
if isinstance(f, CachedReferenceField):
|
if isinstance(f, CachedReferenceField):
|
||||||
|
|
||||||
|
@ -166,7 +166,9 @@ class ValidatorErrorTest(unittest.TestCase):
|
|||||||
self.assertRaises(ValidationError, lambda: d2.validate())
|
self.assertRaises(ValidationError, lambda: d2.validate())
|
||||||
|
|
||||||
def test_parent_reference_in_child_document(self):
|
def test_parent_reference_in_child_document(self):
|
||||||
""" Test to demonstrate behavior in Issue #954
|
"""
|
||||||
|
Test to ensure a ReferenceField can store a reference to a parent
|
||||||
|
class when inherited. Issue #954.
|
||||||
"""
|
"""
|
||||||
class Parent(Document):
|
class Parent(Document):
|
||||||
meta = {'allow_inheritance': True}
|
meta = {'allow_inheritance': True}
|
||||||
@ -179,14 +181,14 @@ class ValidatorErrorTest(unittest.TestCase):
|
|||||||
parent.save()
|
parent.save()
|
||||||
|
|
||||||
child = Child(reference=parent)
|
child = Child(reference=parent)
|
||||||
try:
|
|
||||||
# Saving child should not raise a ValidationError
|
|
||||||
child.save()
|
|
||||||
except ValidationError as e:
|
|
||||||
self.fail("test should not throw validation error. %s" % e.message)
|
|
||||||
|
|
||||||
def test_parent_reference_set_as_attribute_in_child_document_(self):
|
# Saving child should not raise a ValidationError
|
||||||
""" Test to demonstrate behavior (when set as attribute) in Issue #954
|
child.save()
|
||||||
|
|
||||||
|
def test_parent_reference_set_as_attribute_in_child_document(self):
|
||||||
|
"""
|
||||||
|
Test to ensure a ReferenceField can store a reference to a parent
|
||||||
|
class when inherited and when set via attribute. Issue #954.
|
||||||
"""
|
"""
|
||||||
class Parent(Document):
|
class Parent(Document):
|
||||||
meta = {'allow_inheritance': True}
|
meta = {'allow_inheritance': True}
|
||||||
@ -201,12 +203,8 @@ class ValidatorErrorTest(unittest.TestCase):
|
|||||||
child = Child()
|
child = Child()
|
||||||
child.reference = parent
|
child.reference = parent
|
||||||
|
|
||||||
try:
|
# Saving the child should not raise a ValidationError
|
||||||
# Saving the child should not raise a ValidationError
|
child.save()
|
||||||
child.save()
|
|
||||||
except ValidationError as e:
|
|
||||||
self.fail("test should not throw validation error. %s" % e.message)
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user