Merge branch 'utapyngo-develop' into develop

This commit is contained in:
Itai Shirav 2020-02-07 12:50:52 +02:00
commit 2d434fe61f
2 changed files with 30 additions and 0 deletions

View File

@ -293,6 +293,8 @@ class QuerySet(object):
self._grouping_with_totals = False
self._fields = model_cls.fields().keys()
self._limits = None
self._limit_by = None
self._limit_by_fields = None
self._distinct = False
self._final = False
@ -332,6 +334,24 @@ class QuerySet(object):
qs._limits = (start, stop - start)
return qs
def limit_by(self, offset_limit, *fields):
if isinstance(offset_limit, six.integer_types):
# Single limit
assert offset_limit >= 0, 'negative limits are not supported'
qs = copy(self)
qs._limit_by = (0, offset_limit)
qs._limit_by_fields = fields
return qs
else:
# Offset, limit
offset = offset_limit[0]
limit = offset_limit[1]
assert offset >= 0 and limit >= 0, 'negative limits are not supported'
qs = copy(self)
qs._limit_by = (offset, limit)
qs._limit_by_fields = fields
return qs
def select_fields_as_sql(self):
"""
Returns the selected fields or expressions as a SQL string.
@ -369,6 +389,10 @@ class QuerySet(object):
if self._limits:
sql += '\nLIMIT %d, %d' % self._limits
if self._limit_by:
sql += '\nLIMIT %d, %d' % self._limit_by
sql += ' BY %s' % comma_join('`%s`' % field for field in self._limit_by_fields)
return sql
def order_by_as_sql(self):

View File

@ -432,6 +432,12 @@ class AggregateTestCase(TestCaseWithData):
qs = Mdl.objects_in(self.database).filter(the__next__number__gt=1)
self.assertEqual(qs.conditions_as_sql(), 'the__next__number > 1')
def test_limit_by(self):
qs = Person.objects_in(self.database).aggregate('first_name', 'last_name', 'height', n='count()').\
order_by('first_name', '-height').limit_by(1, 'first_name')
self.assertEqual(qs.count(), 94)
self.assertEqual(list(qs)[89].last_name, 'Bowen')
Color = Enum('Color', u'red blue green yellow brown white black')