fix chaining of the limit, skip, hint, and batch_size

This commit is contained in:
Stefan Wojcik 2017-02-07 23:43:46 -08:00
parent bad4e6846a
commit 6ac7a8eec6
2 changed files with 22 additions and 3 deletions

View File

@ -759,7 +759,10 @@ class BaseQuerySet(object):
queryset = self.clone()
queryset._limit = n if n != 0 else 1
# Return the new queryset to allow chaining
# If a cursor object has already been created, apply the limit to it.
if queryset._cursor_obj:
queryset._cursor_obj.limit(queryset._limit)
return queryset
def skip(self, n):
@ -770,6 +773,11 @@ class BaseQuerySet(object):
"""
queryset = self.clone()
queryset._skip = n
# If a cursor object has already been created, apply the skip to it.
if queryset._cursor_obj:
queryset._cursor_obj.skip(queryset._skip)
return queryset
def hint(self, index=None):
@ -787,6 +795,11 @@ class BaseQuerySet(object):
"""
queryset = self.clone()
queryset._hint = index
# If a cursor object has already been created, apply the hint to it.
if queryset._cursor_obj:
queryset._cursor_obj.hint(queryset._hint)
return queryset
def batch_size(self, size):
@ -800,6 +813,11 @@ class BaseQuerySet(object):
"""
queryset = self.clone()
queryset._batch_size = size
# If a cursor object has already been created, apply the batch size to it.
if queryset._cursor_obj:
queryset._cursor_obj.batch_size(queryset._batch_size)
return queryset
def distinct(self, field):

View File

@ -171,8 +171,9 @@ class QuerySetTest(unittest.TestCase):
# Test slice limit and skip on an existing queryset
people = self.Person.objects
self.assertEqual(len(people), 3)
self.assertEqual(people[1:2], 1)
self.assertEqual(people[0].name, 'User B')
people2 = people[1:2]
self.assertEqual(len(people2), 1)
self.assertEqual(people2[0].name, 'User B')
# Test slice limit and skip cursor reset
qs = self.Person.objects[1:2]