Fixes recursion error when resetting changed fields
Fixes #214 - thanks to wpjunior for the test case
This commit is contained in:
parent
8e1d701c27
commit
556e620c7a
@ -162,15 +162,18 @@ class Document(BaseDocument):
|
|||||||
id_field = self._meta['id_field']
|
id_field = self._meta['id_field']
|
||||||
self[id_field] = self._fields[id_field].to_python(object_id)
|
self[id_field] = self._fields[id_field].to_python(object_id)
|
||||||
|
|
||||||
def reset_changed_fields(doc):
|
def reset_changed_fields(doc, inspected_docs=None):
|
||||||
"""Loop through and reset changed fields lists"""
|
"""Loop through and reset changed fields lists"""
|
||||||
|
|
||||||
|
inspected_docs = inspected_docs or []
|
||||||
|
inspected_docs.append(doc)
|
||||||
if hasattr(doc, '_changed_fields'):
|
if hasattr(doc, '_changed_fields'):
|
||||||
doc._changed_fields = []
|
doc._changed_fields = []
|
||||||
|
|
||||||
for field_name in doc._fields:
|
for field_name in doc._fields:
|
||||||
field = getattr(doc, field_name)
|
field = getattr(doc, field_name)
|
||||||
if hasattr(field, '_changed_fields') and field != doc:
|
if field not in inspected_docs and hasattr(field, '_changed_fields'):
|
||||||
reset_changed_fields(field)
|
reset_changed_fields(field, inspected_docs)
|
||||||
|
|
||||||
reset_changed_fields(self)
|
reset_changed_fields(self)
|
||||||
signals.post_save.send(self.__class__, document=self, created=created)
|
signals.post_save.send(self.__class__, document=self, created=created)
|
||||||
|
@ -1045,6 +1045,32 @@ class DocumentTest(unittest.TestCase):
|
|||||||
except ValidationError:
|
except ValidationError:
|
||||||
self.fail()
|
self.fail()
|
||||||
|
|
||||||
|
def test_save_max_recursion_not_hit(self):
|
||||||
|
|
||||||
|
class Person(Document):
|
||||||
|
name = StringField()
|
||||||
|
parent = ReferenceField('self')
|
||||||
|
friend = ReferenceField('self')
|
||||||
|
|
||||||
|
Person.drop_collection()
|
||||||
|
|
||||||
|
p1 = Person(name="Wilson Jr")
|
||||||
|
p1.parent = None
|
||||||
|
p1.save()
|
||||||
|
|
||||||
|
p2 = Person(name="Wilson Jr2")
|
||||||
|
p2.parent = p1
|
||||||
|
p2.save()
|
||||||
|
|
||||||
|
p1.friend = p2
|
||||||
|
p1.save()
|
||||||
|
|
||||||
|
# Confirm can save and it resets the changed fields without hitting
|
||||||
|
# max recursion error
|
||||||
|
p0 = Person.objects.first()
|
||||||
|
p0.name = 'wpjunior'
|
||||||
|
p0.save()
|
||||||
|
|
||||||
def test_update(self):
|
def test_update(self):
|
||||||
"""Ensure that an existing document is updated instead of be overwritten.
|
"""Ensure that an existing document is updated instead of be overwritten.
|
||||||
"""
|
"""
|
||||||
|
Loading…
x
Reference in New Issue
Block a user