diff options
| author | Anubhav Joshi <anubhav9042@gmail.com> | 2014-05-19 14:15:55 +0530 |
|---|---|---|
| committer | Tim Graham <timograham@gmail.com> | 2014-06-05 13:12:01 -0400 |
| commit | 5643a3b51be338196d0b292d5626ad43648448d3 (patch) | |
| tree | 70651b23bb39652b9ffb869281a77bfaaf9badbb /tests/contenttypes_tests/tests.py | |
| parent | 4f72e5f03ae803fd9faa1445245c1050ca004526 (diff) | |
Fixed #10811 -- Made assigning unsaved objects to FK, O2O, and GFK raise ValueError.
This prevents silent data loss.
Thanks Aymeric Augustin for the initial patch and Loic Bistuer for the review.
Diffstat (limited to 'tests/contenttypes_tests/tests.py')
| -rw-r--r-- | tests/contenttypes_tests/tests.py | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/tests/contenttypes_tests/tests.py b/tests/contenttypes_tests/tests.py index 18c542bf4b..2bec2927bd 100644 --- a/tests/contenttypes_tests/tests.py +++ b/tests/contenttypes_tests/tests.py @@ -209,6 +209,27 @@ 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, 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 + class GenericRelationshipTests(IsolatedModelsTestCase): |
