From 7ce9644d9347455ae6f9bd383788a65e4bcadda3 Mon Sep 17 00:00:00 2001 From: Claude Paroz Date: Wed, 24 Dec 2014 14:13:58 +0100 Subject: [PATCH] Added a test to ensure bad assignation to M2M doesn't clear data Refs #14394. --- tests/m2m_regress/tests.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/tests/m2m_regress/tests.py b/tests/m2m_regress/tests.py index e795e06594..b1adc53952 100644 --- a/tests/m2m_regress/tests.py +++ b/tests/m2m_regress/tests.py @@ -97,3 +97,15 @@ class M2MRegressionTests(TestCase): # causes a TypeError in add_lazy_relation m1 = RegressionModelSplit(name='1') m1.save() + + def test_assigning_invalid_data_to_m2m_doesnt_clear_existing_relations(self): + t1 = Tag.objects.create(name='t1') + t2 = Tag.objects.create(name='t2') + c1 = TagCollection.objects.create(name='c1') + c1.tags = [t1, t2] + + with self.assertRaises(TypeError): + c1.tags = 7 + + c1.refresh_from_db() + self.assertQuerysetEqual(c1.tags.order_by('name'), ["", ""])