Better implementation for none - MongoEngine/mongoengine#127
This commit is contained in:
parent
8706fbe461
commit
e7c0da38c2
@ -39,6 +39,7 @@ class QuerySet(object):
|
|||||||
providing :class:`~mongoengine.Document` objects as the results.
|
providing :class:`~mongoengine.Document` objects as the results.
|
||||||
"""
|
"""
|
||||||
__dereference = False
|
__dereference = False
|
||||||
|
__none = False
|
||||||
|
|
||||||
def __init__(self, document, collection):
|
def __init__(self, document, collection):
|
||||||
self._document = document
|
self._document = document
|
||||||
@ -391,7 +392,7 @@ class QuerySet(object):
|
|||||||
"""
|
"""
|
||||||
self._iter = True
|
self._iter = True
|
||||||
try:
|
try:
|
||||||
if self._limit == 0:
|
if self._limit == 0 or self.__none:
|
||||||
raise StopIteration
|
raise StopIteration
|
||||||
if self._scalar:
|
if self._scalar:
|
||||||
return self._get_scalar(self._document._from_son(
|
return self._get_scalar(self._document._from_son(
|
||||||
@ -411,7 +412,8 @@ class QuerySet(object):
|
|||||||
|
|
||||||
def none(self):
|
def none(self):
|
||||||
"""Helper that just returns a list"""
|
"""Helper that just returns a list"""
|
||||||
return []
|
self.__none = True
|
||||||
|
return self
|
||||||
|
|
||||||
def count(self):
|
def count(self):
|
||||||
"""Count the selected elements in the query.
|
"""Count the selected elements in the query.
|
||||||
|
@ -258,7 +258,9 @@ class QuerySetTest(unittest.TestCase):
|
|||||||
|
|
||||||
A.drop_collection()
|
A.drop_collection()
|
||||||
A().save()
|
A().save()
|
||||||
self.assertEqual(A.objects.none(), [])
|
|
||||||
|
self.assertEqual(list(A.objects.none()), [])
|
||||||
|
self.assertEqual(list(A.objects.none().all()), [])
|
||||||
|
|
||||||
def test_chaining(self):
|
def test_chaining(self):
|
||||||
class A(Document):
|
class A(Document):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user