[1.8.x] Fixed #24495 -- Allowed unsaved model instance assignment check to be bypassed.

Backport of 81e1a35c36 from master
This commit is contained in:
Karl Hobley 2015-03-16 19:28:53 +00:00 committed by Tim Graham
parent 247251c2e1
commit a8c53041f9
10 changed files with 136 additions and 5 deletions

View file

@ -32,6 +32,8 @@ class GenericForeignKey(object):
one_to_one = False
related_model = None
allow_unsaved_instance_assignment = False
def __init__(self, ct_field="content_type", fk_field="object_id", for_concrete_model=True):
self.ct_field = ct_field
self.fk_field = fk_field
@ -241,7 +243,7 @@ class GenericForeignKey(object):
if value is not None:
ct = self.get_content_type(obj=value)
fk = value._get_pk_val()
if fk is None:
if not self.allow_unsaved_instance_assignment and fk is None:
raise ValueError(
'Cannot assign "%r": "%s" instance isn\'t saved in the database.' %
(value, value._meta.object_name)