QuerySet limit function now returns all docs in cursor when 0 is passed
This commit is contained in:
parent
3794b181d5
commit
2a795e9138
@ -4,6 +4,7 @@ Changelog
|
|||||||
|
|
||||||
Development
|
Development
|
||||||
===========
|
===========
|
||||||
|
- QuerySet limit function behaviour: Passing 0 as parameter will return all the documents in the cursor #1611
|
||||||
- (Fill this out as you fix issues and develop your features).
|
- (Fill this out as you fix issues and develop your features).
|
||||||
|
|
||||||
Changes in 0.14.0
|
Changes in 0.14.0
|
||||||
|
@ -384,7 +384,7 @@ class BaseQuerySet(object):
|
|||||||
:meth:`skip` that has been applied to this cursor into account when
|
:meth:`skip` that has been applied to this cursor into account when
|
||||||
getting the count
|
getting the count
|
||||||
"""
|
"""
|
||||||
if self._limit == 0 and with_limit_and_skip or self._none:
|
if self._limit == 0 and with_limit_and_skip is False or self._none:
|
||||||
return 0
|
return 0
|
||||||
return self._cursor.count(with_limit_and_skip=with_limit_and_skip)
|
return self._cursor.count(with_limit_and_skip=with_limit_and_skip)
|
||||||
|
|
||||||
@ -759,10 +759,11 @@ class BaseQuerySet(object):
|
|||||||
"""Limit the number of returned documents to `n`. This may also be
|
"""Limit the number of returned documents to `n`. This may also be
|
||||||
achieved using array-slicing syntax (e.g. ``User.objects[:5]``).
|
achieved using array-slicing syntax (e.g. ``User.objects[:5]``).
|
||||||
|
|
||||||
:param n: the maximum number of objects to return
|
:param n: the maximum number of objects to return if n is greater than 0.
|
||||||
|
When 0 is passed, returns all the documents in the cursor
|
||||||
"""
|
"""
|
||||||
queryset = self.clone()
|
queryset = self.clone()
|
||||||
queryset._limit = n if n != 0 else 1
|
queryset._limit = n
|
||||||
|
|
||||||
# If a cursor object has already been created, apply the limit to it.
|
# If a cursor object has already been created, apply the limit to it.
|
||||||
if queryset._cursor_obj:
|
if queryset._cursor_obj:
|
||||||
|
@ -124,6 +124,11 @@ class QuerySetTest(unittest.TestCase):
|
|||||||
self.assertEqual(len(people2), 1)
|
self.assertEqual(len(people2), 1)
|
||||||
self.assertEqual(people2[0], user_a)
|
self.assertEqual(people2[0], user_a)
|
||||||
|
|
||||||
|
# Test limit with 0 as parameter
|
||||||
|
people = self.Person.objects.limit(0)
|
||||||
|
self.assertEqual(people.count(with_limit_and_skip=True), 2)
|
||||||
|
self.assertEqual(len(people), 2)
|
||||||
|
|
||||||
# Test chaining of only after limit
|
# Test chaining of only after limit
|
||||||
person = self.Person.objects().limit(1).only('name').first()
|
person = self.Person.objects().limit(1).only('name').first()
|
||||||
self.assertEqual(person, user_a)
|
self.assertEqual(person, user_a)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user