Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
2 changes: 2 additions & 0 deletions django/db/models/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -1338,6 +1338,8 @@ def update(self, **kwargs):
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)
Expand Down
6 changes: 6 additions & 0 deletions tests/distinct_on_fields/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,3 +178,9 @@ def test_distinct_on_mixed_case_annotation(self):
.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")