1
0
mirror of https://github.com/django/django.git synced 2025-06-28 23:09:12 +00:00

[5.2.x] Fixed #36388 -- Made QuerySet.union() return self when called with no arguments.

Regression in 9cb8baa0c4fa2c10789c5c8b65f4465932d4d172.
Thank you to Antoine Humeau for the report and Simon Charette for the review.

Backport of 802baf5da5b8d8b44990a8214a43b951e7ab8b39 from main.
This commit is contained in:
Colleen Dunlap 2025-05-15 15:41:59 -04:00 committed by Sarah Boyce
parent 6228a35095
commit 787f3130f7
4 changed files with 13 additions and 0 deletions

View File

@ -237,6 +237,7 @@ answer newbie questions, and generally made Django that much better:
Ciaran McCormick <ciaran@ciaranmccormick.com>
Claude Paroz <claude@2xlibre.net>
Clint Ecker
Colleen Dunlap <https://medium.com/@colleen85052>
colin@owlfish.com
Colin Wood <cwood06@gmail.com>
Collin Anderson <cmawebsite@gmail.com>

View File

@ -1554,6 +1554,8 @@ class QuerySet(AltersData):
if len(qs) == 1:
return qs[0]
return qs[0]._combinator_query("union", *qs[1:], all=all)
elif not other_qs:
return self
return self._combinator_query("union", *other_qs, all=all)
def intersection(self, *other_qs):

View File

@ -15,3 +15,6 @@ Bugfixes
* Fixed a bug in Django 5.2 where subqueries using ``"pk"`` to reference models
with a ``CompositePrimaryKey`` failed to raise ``ValueError`` when too many
or too few columns were selected (:ticket:`36392`).
* Fixed a regression in Django 5.2 that caused a crash when no arguments were
passed into ``QuerySet.union()`` (:ticket:`36388`).

View File

@ -88,6 +88,13 @@ class QuerySetSetOperationTests(TestCase):
qs3 = qs1.union(qs2)
self.assertNumbersEqual(qs3[:1], [0])
def test_union_empty_slice(self):
qs = Number.objects.union()
self.assertNumbersEqual(qs[:1], [0])
qs = Number.objects.union(all=True)
self.assertNumbersEqual(qs[:1], [0])
self.assertNumbersEqual(qs.order_by("num")[0:], list(range(0, 10)))
def test_union_all_none_slice(self):
qs = Number.objects.filter(id__in=[])
with self.assertNumQueries(0):