Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion mongoengine/queryset/queryset.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ def _iter_results(self):
yield self._result_cache[pos]
pos += 1
if not self._has_more:
raise StopIteration
if pos == len(self):
raise StopIteration
if len(self._result_cache) <= pos:
self._populate_cache()

Expand Down Expand Up @@ -155,6 +156,8 @@ def __iter__(self):
queryset.rewind()
return queryset

def __len__(self):
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Otherwise: TypeError: object of type 'QuerySetNoCache' has no len()

return len([1 for i in self])

class QuerySetNoDeRef(QuerySet):
"""Special no_dereference QuerySet"""
Expand Down
25 changes: 25 additions & 0 deletions tests/queryset/queryset.py
Original file line number Diff line number Diff line change
Expand Up @@ -4822,5 +4822,30 @@ class Doc(Document):

self.assertEqual(1, Doc.objects(item__type__="axe").count())

def test_len_during_iteration(self):
"""
Tests that calling len on a queyset during iteration doesn't stop
paging.
"""

class Data(Document):
pass

for i in xrange(500):
Data().save()

records = Data.objects.limit(250)
len(records)
for i, r in enumerate(records):
if i == 58:
len(records)
self.assertEqual(i, 249)

records = Data.objects.limit(250)
for i, r in enumerate(records):
if i == 58:
len(records)
self.assertEqual(i, 249)

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