summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEgidijus Macijauskas <e.macijauskas@outlook.com>2021-02-09 23:43:01 +0200
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2021-02-11 08:33:30 +0100
commit6307c3f1a123f5975c73b231e8ac4f115fd72c0d (patch)
tree31c0804185c60049ce3171f8b6679f4520584523
parent4e8ecf0cb6ea36c45edb9cb86f0d63224e08097e (diff)
Fixed #32433 -- Added error message on QuerySet.delete() following distinct().
-rw-r--r--AUTHORS1
-rw-r--r--django/db/models/query.py2
-rw-r--r--tests/delete_regress/tests.py13
3 files changed, 15 insertions, 1 deletions
diff --git a/AUTHORS b/AUTHORS
index f5c71c7e30..68783f4cfd 100644
--- a/AUTHORS
+++ b/AUTHORS
@@ -274,6 +274,7 @@ answer newbie questions, and generally made Django that much better:
dusk@woofle.net
Dustyn Gibson <miigotu@gmail.com>
Ed Morley <https://github.com/edmorley>
+ Egidijus Macijauskas <e.macijauskas@outlook.com>
eibaan@gmail.com
elky <http://elky.me/>
Emmanuelle Delescolle <https://github.com/nanuxbe>
diff --git a/django/db/models/query.py b/django/db/models/query.py
index 32fee78e9f..02c1b31435 100644
--- a/django/db/models/query.py
+++ b/django/db/models/query.py
@@ -724,6 +724,8 @@ class QuerySet:
assert not self.query.is_sliced, \
"Cannot use 'limit' or 'offset' with delete."
+ if self.query.distinct or self.query.distinct_fields:
+ raise TypeError('Cannot call delete() after .distinct().')
if self._fields is not None:
raise TypeError("Cannot call delete() after .values() or .values_list()")
diff --git a/tests/delete_regress/tests.py b/tests/delete_regress/tests.py
index e4fa59e89f..97a7d6ba02 100644
--- a/tests/delete_regress/tests.py
+++ b/tests/delete_regress/tests.py
@@ -1,7 +1,9 @@
import datetime
from django.db import connection, models, transaction
-from django.test import TestCase, TransactionTestCase, skipUnlessDBFeature
+from django.test import (
+ SimpleTestCase, TestCase, TransactionTestCase, skipUnlessDBFeature,
+)
from .models import (
Award, AwardNote, Book, Child, Contact, Eaten, Email, File, Food, FooFile,
@@ -352,3 +354,12 @@ class DeleteTests(TestCase):
self.assertEqual(researcher1.secondary_contact, contact2)
self.assertEqual(researcher2.primary_contact, contact2)
self.assertIsNone(researcher2.secondary_contact)
+
+
+class DeleteDistinct(SimpleTestCase):
+ def test_disallowed_delete_distinct(self):
+ msg = 'Cannot call delete() after .distinct().'
+ with self.assertRaisesMessage(TypeError, msg):
+ Book.objects.distinct().delete()
+ with self.assertRaisesMessage(TypeError, msg):
+ Book.objects.distinct('id').delete()