summaryrefslogtreecommitdiff
path: root/tests/contenttypes_tests
diff options
context:
space:
mode:
authorKarl Hobley <karlhobley10@gmail.com>2015-03-16 19:28:53 +0000
committerTim Graham <timograham@gmail.com>2015-03-18 19:00:09 -0400
commit81e1a35c364e5353d2bf99368ad30a4184fbb653 (patch)
tree2cbfcc6a605600d4f4c387d4c49d17dfe300b91f /tests/contenttypes_tests
parent02d78bb1a80706d941ffc6c892cc75208eb6b782 (diff)
Fixed #24495 -- Allowed unsaved model instance assignment check to be bypassed.
Diffstat (limited to 'tests/contenttypes_tests')
-rw-r--r--tests/contenttypes_tests/tests.py27
1 files changed, 27 insertions, 0 deletions
diff --git a/tests/contenttypes_tests/tests.py b/tests/contenttypes_tests/tests.py
index 3ae4774033..ef01a56301 100644
--- a/tests/contenttypes_tests/tests.py
+++ b/tests/contenttypes_tests/tests.py
@@ -257,6 +257,33 @@ class GenericForeignKeyTests(IsolatedModelsTestCase):
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)
+ 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):