diff --git a/AUTHORS b/AUTHORS index 7e287e75ed..28169c3e17 100644 --- a/AUTHORS +++ b/AUTHORS @@ -707,6 +707,7 @@ answer newbie questions, and generally made Django that much better: Matt Dennenbaum Matthew Flanagan Matthew Schinckel + Matthew Shirley Matthew Somerville Matthew Tretter Matthew Wilkes 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")