From f9a08eb8975d8a49bee8cc42ae207100adb6ce75 Mon Sep 17 00:00:00 2001 From: Ian Foote Date: Fri, 6 Nov 2015 20:18:00 +0100 Subject: [1.9.x] Fixed #25693 -- Prevented data loss with Prefetch and ManyToManyField. Thanks to Jamie Matthews for finding and explaining the bug. Backport of 4608573788c04fc047da42b4b7b48fdee8136ad3 from master --- tests/prefetch_related/tests.py | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'tests') diff --git a/tests/prefetch_related/tests.py b/tests/prefetch_related/tests.py index 7aed238bc0..cd763bb950 100644 --- a/tests/prefetch_related/tests.py +++ b/tests/prefetch_related/tests.py @@ -223,6 +223,17 @@ class PrefetchRelatedTests(TestCase): self.assertIn('prefetch_related', str(cm.exception)) self.assertIn("name", str(cm.exception)) + def test_m2m_shadow(self): + msg = 'to_attr=books conflicts with a field on the Author model.' + poems = Book.objects.filter(title='Poems') + with self.assertRaisesMessage(ValueError, msg): + list(Author.objects.prefetch_related( + Prefetch('books', queryset=poems, to_attr='books'), + )) + # Without the ValueError, a book was deleted due to the implicit + # save of reverse relation assignment. + self.assertEqual(self.author1.books.count(), 2) + class CustomPrefetchTests(TestCase): @classmethod -- cgit v1.3