Skip to content

Commit 6307c3f

Browse files
Egidijus Macijauskasfelixxm
Egidijus Macijauskas
authored andcommitted
Fixed #32433 -- Added error message on QuerySet.delete() following distinct().
1 parent 4e8ecf0 commit 6307c3f

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,7 @@ answer newbie questions, and generally made Django that much better:
274274
275275
Dustyn Gibson <[email protected]>
276276
Ed Morley <https://github.com/edmorley>
277+
Egidijus Macijauskas <[email protected]>
277278
278279
elky <http://elky.me/>
279280
Emmanuelle Delescolle <https://github.com/nanuxbe>

django/db/models/query.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -724,6 +724,8 @@ def delete(self):
724724
assert not self.query.is_sliced, \
725725
"Cannot use 'limit' or 'offset' with delete."
726726

727+
if self.query.distinct or self.query.distinct_fields:
728+
raise TypeError('Cannot call delete() after .distinct().')
727729
if self._fields is not None:
728730
raise TypeError("Cannot call delete() after .values() or .values_list()")
729731

tests/delete_regress/tests.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import datetime
22

33
from django.db import connection, models, transaction
4-
from django.test import TestCase, TransactionTestCase, skipUnlessDBFeature
4+
from django.test import (
5+
SimpleTestCase, TestCase, TransactionTestCase, skipUnlessDBFeature,
6+
)
57

68
from .models import (
79
Award, AwardNote, Book, Child, Contact, Eaten, Email, File, Food, FooFile,
@@ -352,3 +354,12 @@ def test_foreign_key_delete_nullifies_correct_columns(self):
352354
self.assertEqual(researcher1.secondary_contact, contact2)
353355
self.assertEqual(researcher2.primary_contact, contact2)
354356
self.assertIsNone(researcher2.secondary_contact)
357+
358+
359+
class DeleteDistinct(SimpleTestCase):
360+
def test_disallowed_delete_distinct(self):
361+
msg = 'Cannot call delete() after .distinct().'
362+
with self.assertRaisesMessage(TypeError, msg):
363+
Book.objects.distinct().delete()
364+
with self.assertRaisesMessage(TypeError, msg):
365+
Book.objects.distinct('id').delete()

0 commit comments

Comments
 (0)