Fixed #24296 -- Made QuerySet.exists() clear selected columns for not sliced distinct querysets.

This commit is contained in:
mgaligniana 2022-04-11 09:25:26 -03:00 committed by Mariusz Felisiak
parent d2263b7b87
commit 0ad5316f22
2 changed files with 9 additions and 1 deletions

View File

@ -562,7 +562,7 @@ class Query(BaseExpression):
def exists(self, using, limit=True):
q = self.clone()
if not q.distinct:
if not (q.distinct and q.is_sliced):
if q.group_by is True:
q.add_fields(
(f.attname for f in self.model._meta.concrete_fields), False

View File

@ -2226,6 +2226,14 @@ class ExistsSql(TestCase):
self.assertNotIn(id, qstr)
self.assertNotIn(name, qstr)
def test_distinct_exists(self):
with CaptureQueriesContext(connection) as captured_queries:
self.assertIs(Article.objects.distinct().exists(), False)
self.assertEqual(len(captured_queries), 1)
captured_sql = captured_queries[0]["sql"]
self.assertNotIn(connection.ops.quote_name("id"), captured_sql)
self.assertNotIn(connection.ops.quote_name("name"), captured_sql)
def test_sliced_distinct_exists(self):
with CaptureQueriesContext(connection) as captured_queries:
self.assertIs(Article.objects.distinct()[1:3].exists(), False)