Fixed #18414 -- qs.exists() for sliced distinct queries

This commit is contained in:
Anssi Kääriäinen 2013-10-05 21:40:36 +03:00
parent 068b1e045f
commit 93cc6dcdac
2 changed files with 21 additions and 1 deletions

View File

@ -431,7 +431,8 @@ class Query(object):
def has_results(self, using):
q = self.clone()
q.clear_select_clause()
if not q.distinct:
q.clear_select_clause()
q.clear_ordering(True)
q.set_limits(high=1)
compiler = q.get_compiler(using=using)

View File

@ -1856,6 +1856,25 @@ class ExistsSql(TestCase):
id, name = connection.ops.quote_name('id'), connection.ops.quote_name('name')
self.assertTrue(id not in qstr and name not in qstr)
def test_ticket_18414(self):
Article.objects.create(name='one', created=datetime.datetime.now())
Article.objects.create(name='one', created=datetime.datetime.now())
Article.objects.create(name='two', created=datetime.datetime.now())
self.assertTrue(Article.objects.exists())
self.assertTrue(Article.objects.distinct().exists())
self.assertTrue(Article.objects.distinct()[1:3].exists())
self.assertFalse(Article.objects.distinct()[1:1].exists())
@unittest.skipUnless(connection.features.can_distinct_on_fields,
'Uses distinct(fields)')
def test_ticket_18414_distinct_on(self):
Article.objects.create(name='one', created=datetime.datetime.now())
Article.objects.create(name='one', created=datetime.datetime.now())
Article.objects.create(name='two', created=datetime.datetime.now())
self.assertTrue(Article.objects.distinct('name').exists())
self.assertTrue(Article.objects.distinct('name')[1:2].exists())
self.assertFalse(Article.objects.distinct('name')[2:3].exists())
class QuerysetOrderedTests(unittest.TestCase):
"""