Fixed #36683 -- Added error message on QuerySet.update() following distinct(*fields).
Some checks failed
Linters / flake8 (push) Has been cancelled
Linters / isort (push) Has been cancelled
Linters / black (push) Has been cancelled
Tests / Windows, SQLite, Python 3.14 (push) Has been cancelled
Tests / JavaScript tests (push) Has been cancelled

This commit is contained in:
Matthew Shirley 2025-10-23 11:52:32 -07:00 committed by Jacob Walls
parent 3ff32c50d1
commit 4744e9939b
3 changed files with 9 additions and 0 deletions

View file

@ -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>

View file

@ -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)

View file

@ -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")