diff options
| author | Matthew Shirley <matt@greenspacehealth.com> | 2025-10-23 11:52:32 -0700 |
|---|---|---|
| committer | Jacob Walls <jacobtylerwalls@gmail.com> | 2025-10-25 12:21:27 -0400 |
| commit | 4744e9939b65d168c531e5e23d1ac8a4445ac7f9 (patch) | |
| tree | e81440ae873d0dfa80aef5662af2ef6c9a12020e | |
| parent | 3ff32c50d143d8a498f9a5dfef1a31b16a7456fe (diff) | |
Fixed #36683 -- Added error message on QuerySet.update() following distinct(*fields).
| -rw-r--r-- | AUTHORS | 1 | ||||
| -rw-r--r-- | django/db/models/query.py | 2 | ||||
| -rw-r--r-- | tests/distinct_on_fields/tests.py | 6 |
3 files changed, 9 insertions, 0 deletions
@@ -707,6 +707,7 @@ answer newbie questions, and generally made Django that much better: Matt Dennenbaum Matthew Flanagan <https://wadofstuff.blogspot.com/> Matthew Schinckel <matt@schinckel.net> + Matthew Shirley <matt@mattshirley.net> Matthew Somerville <matthew-django@dracos.co.uk> Matthew Tretter <m@tthewwithanm.com> Matthew Wilkes <matt@matthewwilkes.name> diff --git a/django/db/models/query.py b/django/db/models/query.py index a2af672546..70177667a6 100644 --- a/django/db/models/query.py +++ b/django/db/models/query.py @@ -1338,6 +1338,8 @@ class QuerySet(AltersData): self._not_support_combined_queries("update") if self.query.is_sliced: raise TypeError("Cannot update a query once a slice has been taken.") + if self.query.distinct_fields: + raise TypeError("Cannot call update() after .distinct(*fields).") self._for_write = True query = self.query.chain(sql.UpdateQuery) query.add_update_values(kwargs) diff --git a/tests/distinct_on_fields/tests.py b/tests/distinct_on_fields/tests.py index 93b3f27aec..f03e05ac73 100644 --- a/tests/distinct_on_fields/tests.py +++ b/tests/distinct_on_fields/tests.py @@ -178,3 +178,9 @@ class DistinctOnTests(TestCase): .order_by("nAmEAlIaS") ) self.assertSequenceEqual(qs, [self.p1_o1, self.p2_o1, self.p3_o1]) + + def test_disallowed_update_distinct_on(self): + qs = Staff.objects.distinct("organisation").order_by("organisation") + msg = "Cannot call update() after .distinct(*fields)." + with self.assertRaisesMessage(TypeError, msg): + qs.update(name="p4") |
