From 741061852851d6aff8e35f46b0e165685d3e79e0 Mon Sep 17 00:00:00 2001 From: Alexandr Tatarinov Date: Sat, 23 Jun 2018 23:39:22 +0300 Subject: [PATCH] Fixed #29447 -- Made RelatedManager.set() pass bulk argument to clear(). --- AUTHORS | 1 + django/db/models/fields/related_descriptors.py | 2 +- tests/many_to_one_null/tests.py | 5 +++++ 3 files changed, 7 insertions(+), 1 deletion(-) diff --git a/AUTHORS b/AUTHORS index d89c5a2e03..dbb2880613 100644 --- a/AUTHORS +++ b/AUTHORS @@ -35,6 +35,7 @@ answer newbie questions, and generally made Django that much better: Aleksi Häkli Alexander Dutton Alexander Myodov + Alexandr Tatarinov Alex Couper Alex Dedul Alex Gaynor diff --git a/django/db/models/fields/related_descriptors.py b/django/db/models/fields/related_descriptors.py index 4ae4bf5156..e69fd41e8a 100644 --- a/django/db/models/fields/related_descriptors.py +++ b/django/db/models/fields/related_descriptors.py @@ -708,7 +708,7 @@ def create_reverse_many_to_one_manager(superclass, rel): db = router.db_for_write(self.model, instance=self.instance) with transaction.atomic(using=db, savepoint=False): if clear: - self.clear() + self.clear(bulk=bulk) self.add(*objs, bulk=bulk) else: old_objs = set(self.using(db).all()) diff --git a/tests/many_to_one_null/tests.py b/tests/many_to_one_null/tests.py index 77b8fd7c25..6ccabefe66 100644 --- a/tests/many_to_one_null/tests.py +++ b/tests/many_to_one_null/tests.py @@ -85,6 +85,11 @@ class ManyToOneNullTests(TestCase): ['', '', ''] ) + def test_set_clear_non_bulk(self): + # 2 queries for clear(), 1 for add(), and 1 to select objects. + with self.assertNumQueries(4): + self.r.article_set.set([self.a], bulk=False, clear=True) + def test_assign_clear_related_set(self): # Use descriptor assignment to allocate ForeignKey. Null is legal, so # existing members of the set that are not in the assignment set are