mirror of
https://github.com/django/django.git
synced 2025-09-26 20:19:16 +00:00
#fixed #2256 -- Made count() interact with slicing on QuerySets. Patch from
SmileyChris. git-svn-id: http://code.djangoproject.com/svn/django/trunk@4488 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
80e58b3211
commit
d123588184
2 changed files with 25 additions and 2 deletions
|
@ -197,9 +197,12 @@ class QuerySet(object):
|
|||
"Performs a SELECT COUNT() and returns the number of records as an integer."
|
||||
counter = self._clone()
|
||||
counter._order_by = ()
|
||||
counter._select_related = False
|
||||
|
||||
offset = counter._offset
|
||||
limit = counter._limit
|
||||
counter._offset = None
|
||||
counter._limit = None
|
||||
counter._select_related = False
|
||||
|
||||
try:
|
||||
select, sql, params = counter._get_sql_clause()
|
||||
|
@ -213,7 +216,16 @@ class QuerySet(object):
|
|||
cursor.execute("SELECT COUNT(DISTINCT(%s))" % id_col + sql, params)
|
||||
else:
|
||||
cursor.execute("SELECT COUNT(*)" + sql, params)
|
||||
return cursor.fetchone()[0]
|
||||
count = cursor.fetchone()[0]
|
||||
|
||||
# Apply any offset and limit constraints manually, since using LIMIT or
|
||||
# OFFSET in SQL doesn't change the output of COUNT.
|
||||
if offset:
|
||||
count = max(0, count - offset)
|
||||
if limit:
|
||||
count = min(limit, count)
|
||||
|
||||
return count
|
||||
|
||||
def get(self, *args, **kwargs):
|
||||
"Performs the SELECT and returns a single object matching the given keyword arguments."
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue