From f2d5417d3b7a4012298fcffc6eab618cc09d0344 Mon Sep 17 00:00:00 2001 From: Paulo Date: Sun, 29 Oct 2017 12:01:04 +0100 Subject: [PATCH] [2.0.x] Fixed #28742 -- Fixed AttributeError crash when assigning None to cached reverse relations. Backport of fcfcf8aae470d893b0d2ef176434461edf9e9c4d from master --- django/db/models/fields/related_descriptors.py | 11 ++++------- tests/one_to_one/tests.py | 9 +++++++++ 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/django/db/models/fields/related_descriptors.py b/django/db/models/fields/related_descriptors.py index 39d7223d8a..1cce498d3d 100644 --- a/django/db/models/fields/related_descriptors.py +++ b/django/db/models/fields/related_descriptors.py @@ -410,13 +410,10 @@ class ReverseOneToOneDescriptor: if value is None: # Update the cached related instance (if any) & clear the cache. - try: - # Following the example above, this would be the cached - # ``restaurant`` instance (if any). - rel_obj = self.related.get_cached_value(instance) - except KeyError: - pass - else: + # Following the example above, this would be the cached + # ``restaurant`` instance (if any). + rel_obj = self.related.get_cached_value(instance, default=None) + if rel_obj is not None: # Remove the ``restaurant`` instance from the ``place`` # instance cache. self.related.delete_cached_value(instance) diff --git a/tests/one_to_one/tests.py b/tests/one_to_one/tests.py index 65257cf7dc..314c56a60f 100644 --- a/tests/one_to_one/tests.py +++ b/tests/one_to_one/tests.py @@ -196,6 +196,15 @@ class OneToOneTests(TestCase): # UndergroundBar. p.undergroundbar = None + def test_assign_none_to_null_cached_reverse_relation(self): + p = Place.objects.get(name='Demon Dogs') + # Prime the relation's cache with a value of None. + with self.assertRaises(Place.undergroundbar.RelatedObjectDoesNotExist): + getattr(p, 'undergroundbar') + # Assigning None works if there isn't a related UndergroundBar and the + # reverse cache has a value of None. + p.undergroundbar = None + def test_related_object_cache(self): """ Regression test for #6886 (the related-object cache) """