From 5980b05c1fad69eef907e0076aa2dc837edab529 Mon Sep 17 00:00:00 2001 From: Tim Graham Date: Fri, 24 Jul 2015 07:51:40 -0400 Subject: Fixed #25160 -- Moved unsaved model instance data loss check to Model.save() This mostly reverts 5643a3b51be338196d0b292d5626ad43648448d3 and 81e1a35c364e5353d2bf99368ad30a4184fbb653. Thanks Carl Meyer for review. --- tests/contenttypes_tests/tests.py | 48 --------------------------------------- 1 file changed, 48 deletions(-) (limited to 'tests/contenttypes_tests') diff --git a/tests/contenttypes_tests/tests.py b/tests/contenttypes_tests/tests.py index 3ff613d9c4..0d0f9d30b8 100644 --- a/tests/contenttypes_tests/tests.py +++ b/tests/contenttypes_tests/tests.py @@ -236,54 +236,6 @@ class GenericForeignKeyTests(IsolatedModelsTestCase): errors = checks.run_checks() self.assertEqual(errors, ['performed!']) - def test_unsaved_instance_on_generic_foreign_key(self): - """ - #10811 -- Assigning an unsaved object to GenericForeignKey - should raise an exception. - """ - class Model(models.Model): - content_type = models.ForeignKey(ContentType, models.SET_NULL, null=True) - object_id = models.PositiveIntegerField(null=True) - content_object = GenericForeignKey('content_type', 'object_id') - - author = Author(name='Author') - model = Model() - model.content_object = None # no error here as content_type allows None - with self.assertRaisesMessage(ValueError, - 'Cannot assign "%r": "%s" instance isn\'t saved in the database.' - % (author, author._meta.object_name)): - model.content_object = author # raised ValueError here as author is unsaved - - author.save() - model.content_object = author # no error because the instance is saved - - def test_unsaved_instance_on_generic_foreign_key_allowed_when_wanted(self): - """ - #24495 - Assigning an unsaved object to a GenericForeignKey - should be allowed when the allow_unsaved_instance_assignment - attribute has been set to True. - """ - class UnsavedGenericForeignKey(GenericForeignKey): - # A GenericForeignKey which can point to an unsaved object - allow_unsaved_instance_assignment = True - - class Band(models.Model): - name = models.CharField(max_length=50) - - class BandMember(models.Model): - band_ct = models.ForeignKey(ContentType, models.CASCADE) - band_id = models.PositiveIntegerField() - band = UnsavedGenericForeignKey('band_ct', 'band_id') - first_name = models.CharField(max_length=50) - last_name = models.CharField(max_length=50) - - beatles = Band(name='The Beatles') - john = BandMember(first_name='John', last_name='Lennon') - # This should not raise an exception as the GenericForeignKey between - # member and band has allow_unsaved_instance_assignment=True. - john.band = beatles - self.assertEqual(john.band, beatles) - class GenericRelationshipTests(IsolatedModelsTestCase): -- cgit v1.3