mirror of
https://github.com/django/django.git
synced 2025-10-30 00:56:09 +00:00
Fixed #12687 -- fixed an issue with aggregates and counts in conjunction with annotations where the QuerySet was provably empty.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@14586 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
@@ -337,7 +337,7 @@ class Query(object):
|
||||
# information but retrieves only the first row. Aggregate
|
||||
# over the subquery instead.
|
||||
if self.group_by is not None:
|
||||
from subqueries import AggregateQuery
|
||||
from django.db.models.sql.subqueries import AggregateQuery
|
||||
query = AggregateQuery(self.model)
|
||||
|
||||
obj = self.clone()
|
||||
@@ -349,7 +349,13 @@ class Query(object):
|
||||
query.aggregate_select[alias] = aggregate
|
||||
del obj.aggregate_select[alias]
|
||||
|
||||
query.add_subquery(obj, using)
|
||||
try:
|
||||
query.add_subquery(obj, using)
|
||||
except EmptyResultSet:
|
||||
return dict(
|
||||
(alias, None)
|
||||
for alias in query.aggregate_select
|
||||
)
|
||||
else:
|
||||
query = self
|
||||
self.select = []
|
||||
@@ -382,13 +388,19 @@ class Query(object):
|
||||
# If a select clause exists, then the query has already started to
|
||||
# specify the columns that are to be returned.
|
||||
# In this case, we need to use a subquery to evaluate the count.
|
||||
from subqueries import AggregateQuery
|
||||
from django.db.models.sql.subqueries import AggregateQuery
|
||||
subquery = obj
|
||||
subquery.clear_ordering(True)
|
||||
subquery.clear_limits()
|
||||
|
||||
obj = AggregateQuery(obj.model)
|
||||
obj.add_subquery(subquery, using=using)
|
||||
try:
|
||||
obj.add_subquery(subquery, using=using)
|
||||
except EmptyResultSet:
|
||||
# add_subquery evaluates the query, if it's an EmptyResultSet
|
||||
# then there are can be no results, and therefore there the
|
||||
# count is obviously 0
|
||||
return 0
|
||||
|
||||
obj.add_count_column()
|
||||
number = obj.get_aggregation(using=using)[None]
|
||||
|
||||
Reference in New Issue
Block a user