diff options
| author | Karl Hobley <karlhobley10@gmail.com> | 2015-03-16 19:28:53 +0000 |
|---|---|---|
| committer | Tim Graham <timograham@gmail.com> | 2015-03-18 19:00:09 -0400 |
| commit | 81e1a35c364e5353d2bf99368ad30a4184fbb653 (patch) | |
| tree | 2cbfcc6a605600d4f4c387d4c49d17dfe300b91f /django | |
| parent | 02d78bb1a80706d941ffc6c892cc75208eb6b782 (diff) | |
Fixed #24495 -- Allowed unsaved model instance assignment check to be bypassed.
Diffstat (limited to 'django')
| -rw-r--r-- | django/contrib/contenttypes/fields.py | 4 | ||||
| -rw-r--r-- | django/db/models/fields/related.py | 5 |
2 files changed, 6 insertions, 3 deletions
diff --git a/django/contrib/contenttypes/fields.py b/django/contrib/contenttypes/fields.py index 1dddbeb41c..f72cd49b26 100644 --- a/django/contrib/contenttypes/fields.py +++ b/django/contrib/contenttypes/fields.py @@ -40,6 +40,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 @@ -250,7 +252,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) diff --git a/django/db/models/fields/related.py b/django/db/models/fields/related.py index 931c117cb8..45c9b15081 100644 --- a/django/db/models/fields/related.py +++ b/django/db/models/fields/related.py @@ -513,7 +513,7 @@ class SingleRelatedObjectDescriptor(object): raise ValueError('Cannot assign "%r": the current database router prevents this relation.' % value) related_pk = tuple(getattr(instance, field.attname) for field in self.related.field.foreign_related_fields) - if None in related_pk: + if not self.related.field.allow_unsaved_instance_assignment and None in related_pk: raise ValueError( 'Cannot assign "%r": "%s" instance isn\'t saved in the database.' % (value, instance._meta.object_name) @@ -684,7 +684,7 @@ class ReverseSingleRelatedObjectDescriptor(object): else: for lh_field, rh_field in self.field.related_fields: pk = value._get_pk_val() - if pk is None: + if not self.field.allow_unsaved_instance_assignment and pk is None: raise ValueError( 'Cannot assign "%r": "%s" instance isn\'t saved in the database.' % (value, self.field.rel.to._meta.object_name) @@ -1534,6 +1534,7 @@ class ForeignObject(RelatedField): one_to_many = False one_to_one = False + allow_unsaved_instance_assignment = False requires_unique_target = True related_accessor_class = ForeignRelatedObjectsDescriptor rel_class = ForeignObjectRel |
