summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSimon Charette <charette.s@gmail.com>2015-11-11 00:49:35 -0500
committerSimon Charette <charette.s@gmail.com>2015-11-11 12:25:10 -0500
commitcc8c02fa0fa2119704d1c39ca8509850aef84acc (patch)
tree0bdf0468d0dd702edf325a64f6be69a5914173fe
parent4a9c32f5eece9030c2b568e930cec0c1ba8f1da0 (diff)
Refs #25693 -- Added a regression test for `to_attr` validation on forward m2m.
-rw-r--r--tests/prefetch_related/tests.py13
1 files changed, 12 insertions, 1 deletions
diff --git a/tests/prefetch_related/tests.py b/tests/prefetch_related/tests.py
index cd763bb950..56bd64b5b9 100644
--- a/tests/prefetch_related/tests.py
+++ b/tests/prefetch_related/tests.py
@@ -223,7 +223,18 @@ class PrefetchRelatedTests(TestCase):
self.assertIn('prefetch_related', str(cm.exception))
self.assertIn("name", str(cm.exception))
- def test_m2m_shadow(self):
+ def test_forward_m2m_to_attr_conflict(self):
+ msg = 'to_attr=authors conflicts with a field on the Book model.'
+ authors = Author.objects.all()
+ with self.assertRaisesMessage(ValueError, msg):
+ list(Book.objects.prefetch_related(
+ Prefetch('authors', queryset=authors, to_attr='authors'),
+ ))
+ # Without the ValueError, an author was deleted due to the implicit
+ # save of the relation assignment.
+ self.assertEqual(self.book1.authors.count(), 3)
+
+ def test_reverse_m2m_to_attr_conflict(self):
msg = 'to_attr=books conflicts with a field on the Author model.'
poems = Book.objects.filter(title='Poems')
with self.assertRaisesMessage(ValueError, msg):