QuerySet repr now uses limit and skip
This commit is contained in:
parent
2996f8919d
commit
634f771547
@ -126,7 +126,8 @@ class QuerySet(object):
|
|||||||
if document._meta.get('allow_inheritance'):
|
if document._meta.get('allow_inheritance'):
|
||||||
self._query = {'_types': self._document._class_name}
|
self._query = {'_types': self._document._class_name}
|
||||||
self._cursor_obj = None
|
self._cursor_obj = None
|
||||||
self._zero_limit = False
|
self._limit = None
|
||||||
|
self._skip = None
|
||||||
|
|
||||||
def ensure_index(self, key_or_list):
|
def ensure_index(self, key_or_list):
|
||||||
"""Ensure that the given indexes are in place.
|
"""Ensure that the given indexes are in place.
|
||||||
@ -381,16 +382,16 @@ class QuerySet(object):
|
|||||||
def next(self):
|
def next(self):
|
||||||
"""Wrap the result in a :class:`~mongoengine.Document` object.
|
"""Wrap the result in a :class:`~mongoengine.Document` object.
|
||||||
"""
|
"""
|
||||||
if self._zero_limit:
|
if self._limit == 0:
|
||||||
raise StopIteration
|
raise StopIteration
|
||||||
return self._document._from_son(self._cursor.next())
|
return self._document._from_son(self._cursor.next())
|
||||||
|
|
||||||
def count(self):
|
def count(self):
|
||||||
"""Count the selected elements in the query.
|
"""Count the selected elements in the query.
|
||||||
"""
|
"""
|
||||||
if self._zero_limit:
|
if self._limit == 0:
|
||||||
return 0
|
return 0
|
||||||
return self._cursor.count()
|
return self._cursor.count(with_limit_and_skip=True)
|
||||||
|
|
||||||
def __len__(self):
|
def __len__(self):
|
||||||
return self.count()
|
return self.count()
|
||||||
@ -402,11 +403,10 @@ class QuerySet(object):
|
|||||||
:param n: the maximum number of objects to return
|
:param n: the maximum number of objects to return
|
||||||
"""
|
"""
|
||||||
if n == 0:
|
if n == 0:
|
||||||
self._zero_limit = True
|
|
||||||
self._cursor.limit(1)
|
self._cursor.limit(1)
|
||||||
else:
|
else:
|
||||||
self._zero_limit = False
|
|
||||||
self._cursor.limit(n)
|
self._cursor.limit(n)
|
||||||
|
self._limit = n
|
||||||
# Return self to allow chaining
|
# Return self to allow chaining
|
||||||
return self
|
return self
|
||||||
|
|
||||||
@ -417,6 +417,7 @@ class QuerySet(object):
|
|||||||
:param n: the number of objects to skip before returning results
|
:param n: the number of objects to skip before returning results
|
||||||
"""
|
"""
|
||||||
self._cursor.skip(n)
|
self._cursor.skip(n)
|
||||||
|
self._skip = n
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def __getitem__(self, key):
|
def __getitem__(self, key):
|
||||||
@ -426,12 +427,15 @@ class QuerySet(object):
|
|||||||
if isinstance(key, slice):
|
if isinstance(key, slice):
|
||||||
try:
|
try:
|
||||||
self._cursor_obj = self._cursor[key]
|
self._cursor_obj = self._cursor[key]
|
||||||
|
self._skip, self._limit = key.start, key.stop
|
||||||
except IndexError, err:
|
except IndexError, err:
|
||||||
# PyMongo raises an error if key.start == key.stop, catch it,
|
# PyMongo raises an error if key.start == key.stop, catch it,
|
||||||
# bin it, kill it.
|
# bin it, kill it.
|
||||||
if key.start >= 0 and key.stop >= 0 and key.step is None:
|
start = key.start or 0
|
||||||
if key.start == key.stop:
|
if start >= 0 and key.stop >= 0 and key.step is None:
|
||||||
|
if start == key.stop:
|
||||||
self.limit(0)
|
self.limit(0)
|
||||||
|
self._skip, self._limit = key.start, key.stop - start
|
||||||
return self
|
return self
|
||||||
raise err
|
raise err
|
||||||
# Allow further QuerySet modifications to be performed
|
# Allow further QuerySet modifications to be performed
|
||||||
@ -679,11 +683,15 @@ class QuerySet(object):
|
|||||||
return self.exec_js(freq_func, list_field, normalize=normalize)
|
return self.exec_js(freq_func, list_field, normalize=normalize)
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
data = list(self[:REPR_OUTPUT_SIZE + 1])
|
limit = REPR_OUTPUT_SIZE + 1
|
||||||
|
if self._limit is not None and self._limit < limit:
|
||||||
|
limit = self._limit
|
||||||
|
data = list(self[self._skip:limit])
|
||||||
if len(data) > REPR_OUTPUT_SIZE:
|
if len(data) > REPR_OUTPUT_SIZE:
|
||||||
data[-1] = "...(remaining elements truncated)..."
|
data[-1] = "...(remaining elements truncated)..."
|
||||||
return repr(data)
|
return repr(data)
|
||||||
|
|
||||||
|
|
||||||
class InvalidCollectionError(Exception):
|
class InvalidCollectionError(Exception):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user