summaryrefslogtreecommitdiff
path: root/tests/many_to_many
diff options
context:
space:
mode:
authorLoic Bistuer <loic.bistuer@gmail.com>2014-03-27 19:32:48 +0800
committerLoic Bistuer <loic.bistuer@gmail.com>2014-03-30 15:36:45 +0700
commit20399083f49faceef1e48530822137257bca9d6a (patch)
tree6f1c65e9284e8f9a1489c99cc0045bfbe7301dc7 /tests/many_to_many
parent2f3e1fe3234f5ebaca7635b0a080c2a751c3c758 (diff)
downloaddjango-20399083f49faceef1e48530822137257bca9d6a.tar.gz
Fixed #19816 -- Pre-evaluate querysets used in direct relation assignments.
Since assignments on M2M or reverse FK descriptors is composed of a `clear()`, followed by an `add()`, `clear()` could potentially affect the value of the assigned queryset before the `add()` step; pre-evaluating it solves the problem. This patch fixes the issue for ForeignRelatedObjectsDescriptor, ManyRelatedObjectsDescriptor, and ReverseGenericRelatedObjectsDescriptor. It completes 6cb6e1 which addressed ReverseManyRelatedObjectsDescriptor.
Diffstat (limited to 'tests/many_to_many')
-rw-r--r--tests/many_to_many/tests.py24
1 files changed, 24 insertions, 0 deletions
diff --git a/tests/many_to_many/tests.py b/tests/many_to_many/tests.py
index 545d50a90e..23c6cd543e 100644
--- a/tests/many_to_many/tests.py
+++ b/tests/many_to_many/tests.py
@@ -370,6 +370,30 @@ class ManyToManyTests(TestCase):
self.assertQuerysetEqual(self.a4.publications.all(),
['<Publication: Science Weekly>'])
+ def test_forward_assign_with_queryset(self):
+ # Ensure that querysets used in m2m assignments are pre-evaluated
+ # so their value isn't affected by the clearing operation in
+ # ManyRelatedObjectsDescriptor.__set__. Refs #19816.
+ self.a1.publications = [self.p1, self.p2]
+
+ qs = self.a1.publications.filter(id=self.a1.id)
+ self.a1.publications = qs
+
+ self.assertEqual(1, self.a1.publications.count())
+ self.assertEqual(1, qs.count())
+
+ def test_reverse_assign_with_queryset(self):
+ # Ensure that querysets used in M2M assignments are pre-evaluated
+ # so their value isn't affected by the clearing operation in
+ # ReverseManyRelatedObjectsDescriptor.__set__. Refs #19816.
+ self.p1.article_set = [self.a1, self.a2]
+
+ qs = self.p1.article_set.filter(id=self.p1.id)
+ self.p1.article_set = qs
+
+ self.assertEqual(1, self.p1.article_set.count())
+ self.assertEqual(1, qs.count())
+
def test_clear(self):
# Relation sets can be cleared:
self.p2.article_set.clear()