From 981a3426cf2f54f5282e79fb7f47726998c87cb2 Mon Sep 17 00:00:00 2001 From: Hasan Ramezani Date: Mon, 5 Oct 2020 11:51:35 +0200 Subject: [PATCH] Fixed #32068 -- Added error messages on get() with filters following union(), intersection(), and difference(). --- django/db/models/query.py | 5 +++++ tests/queries/test_qs_combinators.py | 13 +++++++++++++ 2 files changed, 18 insertions(+) diff --git a/django/db/models/query.py b/django/db/models/query.py index 8d888447cd..50fe2cc29d 100644 --- a/django/db/models/query.py +++ b/django/db/models/query.py @@ -416,6 +416,11 @@ class QuerySet: Perform the query and return a single object matching the given keyword arguments. """ + if self.query.combinator and (args or kwargs): + raise NotSupportedError( + 'Calling QuerySet.get(...) with filters after %s() is not ' + 'supported.' % self.query.combinator + ) clone = self._chain() if self.query.combinator else self.filter(*args, **kwargs) if self.query.can_filter() and not self.query.distinct_fields: clone = clone.order_by() diff --git a/tests/queries/test_qs_combinators.py b/tests/queries/test_qs_combinators.py index 1029e40ed8..5a10196da5 100644 --- a/tests/queries/test_qs_combinators.py +++ b/tests/queries/test_qs_combinators.py @@ -341,3 +341,16 @@ class QuerySetSetOperationTests(TestCase): msg % (operation, combinator), ): getattr(getattr(qs, combinator)(qs), operation)() + + def test_get_with_filters_unsupported_on_combined_qs(self): + qs = Number.objects.all() + msg = 'Calling QuerySet.get(...) with filters after %s() is not supported.' + combinators = ['union'] + if connection.features.supports_select_difference: + combinators.append('difference') + if connection.features.supports_select_intersection: + combinators.append('intersection') + for combinator in combinators: + with self.subTest(combinator=combinator): + with self.assertRaisesMessage(NotSupportedError, msg % combinator): + getattr(qs, combinator)(qs).get(num=2)