summaryrefslogtreecommitdiff
path: root/django/db/models/sql/query.py
diff options
context:
space:
mode:
authorSimon Charette <charette.s@gmail.com>2020-08-12 23:16:22 -0400
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2020-08-13 14:10:36 +0200
commit51297a92324976a704279b567ec4f80bb92d7b60 (patch)
tree4acb395cf9be8778dff721015aeba4cc11654c20 /django/db/models/sql/query.py
parent7f4c9222dfe2f28ff8a7ffc56c28ccbadf19cf6f (diff)
Fixed #31792 -- Made Exists() reuse QuerySet.exists() optimizations.
The latter is already optimized to limit the number of results, avoid selecting unnecessary fields, and drop ordering if possible without altering the semantic of the query.
Diffstat (limited to 'django/db/models/sql/query.py')
-rw-r--r--django/db/models/sql/query.py8
1 files changed, 7 insertions, 1 deletions
diff --git a/django/db/models/sql/query.py b/django/db/models/sql/query.py
index 659fa87314..4648daf395 100644
--- a/django/db/models/sql/query.py
+++ b/django/db/models/sql/query.py
@@ -522,7 +522,7 @@ class Query(BaseExpression):
def has_filters(self):
return self.where
- def has_results(self, using):
+ def exists(self):
q = self.clone()
if not q.distinct:
if q.group_by is True:
@@ -533,6 +533,12 @@ class Query(BaseExpression):
q.clear_select_clause()
q.clear_ordering(True)
q.set_limits(high=1)
+ q.add_extra({'a': 1}, None, None, None, None, None)
+ q.set_extra_mask(['a'])
+ return q
+
+ def has_results(self, using):
+ q = self.exists()
compiler = q.get_compiler(using=using)
return compiler.has_results()