From 7b42d346465abb560d1b774bdf17d094fad75571 Mon Sep 17 00:00:00 2001 From: David-Wobrock Date: Fri, 13 Nov 2020 22:34:16 +0100 Subject: [PATCH] Refs #27718 -- Doc'd and tested QuerySet.exists() for combined querysets. Supported since 84c1826ded17b2d74f66717fb745fc36e37949fd. --- docs/ref/models/querysets.txt | 10 +++++----- tests/queries/test_qs_combinators.py | 23 +++++++++++++++++++++++ 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/docs/ref/models/querysets.txt b/docs/ref/models/querysets.txt index 9e129932b2..c1a8fd1f3d 100644 --- a/docs/ref/models/querysets.txt +++ b/docs/ref/models/querysets.txt @@ -917,11 +917,11 @@ resulting ``QuerySet``. For example:: >>> qs1.union(qs2).order_by('name') In addition, only ``LIMIT``, ``OFFSET``, ``COUNT(*)``, ``ORDER BY``, and -specifying columns (i.e. slicing, :meth:`count`, :meth:`order_by`, and -:meth:`values()`/:meth:`values_list()`) are allowed on the resulting -``QuerySet``. Further, databases place restrictions on what operations are -allowed in the combined queries. For example, most databases don't allow -``LIMIT`` or ``OFFSET`` in the combined queries. +specifying columns (i.e. slicing, :meth:`count`, :meth:`exists`, +:meth:`order_by`, and :meth:`values()`/:meth:`values_list()`) are allowed +on the resulting ``QuerySet``. Further, databases place restrictions on +what operations are allowed in the combined queries. For example, most +databases don't allow ``LIMIT`` or ``OFFSET`` in the combined queries. ``intersection()`` ~~~~~~~~~~~~~~~~~~ diff --git a/tests/queries/test_qs_combinators.py b/tests/queries/test_qs_combinators.py index 58c03f09fe..b12277d34c 100644 --- a/tests/queries/test_qs_combinators.py +++ b/tests/queries/test_qs_combinators.py @@ -254,6 +254,29 @@ class QuerySetSetOperationTests(TestCase): qs2 = Number.objects.filter(num__lte=5) self.assertEqual(qs1.intersection(qs2).count(), 1) + def test_exists_union(self): + qs1 = Number.objects.filter(num__gte=5) + qs2 = Number.objects.filter(num__lte=5) + self.assertIs(qs1.union(qs2).exists(), True) + + def test_exists_union_empty_result(self): + qs = Number.objects.filter(pk__in=[]) + self.assertIs(qs.union(qs).exists(), False) + + @skipUnlessDBFeature('supports_select_intersection') + def test_exists_intersection(self): + qs1 = Number.objects.filter(num__gt=5) + qs2 = Number.objects.filter(num__lt=5) + self.assertIs(qs1.intersection(qs1).exists(), True) + self.assertIs(qs1.intersection(qs2).exists(), False) + + @skipUnlessDBFeature('supports_select_difference') + def test_exists_difference(self): + qs1 = Number.objects.filter(num__gte=5) + qs2 = Number.objects.filter(num__gte=3) + self.assertIs(qs1.difference(qs2).exists(), False) + self.assertIs(qs2.difference(qs1).exists(), True) + def test_get_union(self): qs = Number.objects.filter(num=2) self.assertEqual(qs.union(qs).get().num, 2)