Skip to content

Commit

Permalink
Merge pull request #920 from DavidBord/fix-914
Browse files Browse the repository at this point in the history
ListField of embedded docs doesn't set the _instance attribute when iterating over it
  • Loading branch information
DavidBord committed Apr 2, 2015
2 parents 1001f1b + bb77838 commit a092910
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Changelog

Changes in 0.9.X - DEV
======================
- ListField of embedded docs doesn't set the _instance attribute when iterating over it #914
- Support += and *= for ListField #595
Changes in 0.9.0
Expand Down
4 changes: 4 additions & 0 deletions mongoengine/base/datastructures.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,10 @@ def __getitem__(self, key, *args, **kwargs):
value._instance = self._instance
return value

def __iter__(self):
for i in xrange(self.__len__()):
yield self[i]

def __setitem__(self, key, value, *args, **kwargs):
if isinstance(key, slice):
self._mark_as_changed()
Expand Down
16 changes: 16 additions & 0 deletions tests/document/instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -2799,5 +2799,21 @@ class Person(Document):
self.assertNotEqual(p, p1)
self.assertEqual(p, p)

def test_list_iter(self):
# 914
class B(EmbeddedDocument):
v = StringField()

class A(Document):
l = ListField(EmbeddedDocumentField(B))

A.objects.delete()
A(l=[B(v='1'), B(v='2'), B(v='3')]).save()
a = A.objects.get()
self.assertEqual(a.l._instance, a)
for idx, b in enumerate(a.l):
self.assertEqual(b._instance, a)
self.assertEqual(idx, 2)

if __name__ == '__main__':
unittest.main()

0 comments on commit a092910

Please sign in to comment.