summaryrefslogtreecommitdiff
path: root/django/db/models/sql/query.py
diff options
context:
space:
mode:
Diffstat (limited to 'django/db/models/sql/query.py')
-rw-r--r--django/db/models/sql/query.py20
1 files changed, 16 insertions, 4 deletions
diff --git a/django/db/models/sql/query.py b/django/db/models/sql/query.py
index ec477447f3..c0449e7047 100644
--- a/django/db/models/sql/query.py
+++ b/django/db/models/sql/query.py
@@ -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]