From 5cf96b49e43daea6d4a0ba1c80c45e90c74f4e47 Mon Sep 17 00:00:00 2001 From: Claude Paroz Date: Fri, 27 Feb 2015 18:19:56 +0100 Subject: [PATCH] Fixed #24418 -- Prevented crash in refresh_from_db with null fk Thanks Johannes Lerch for the report, Tim Graham for the test case, and Simon Charette for the review. --- django/db/models/base.py | 2 +- tests/basic/tests.py | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/django/db/models/base.py b/django/db/models/base.py index a505efa87d..cb1d37f9df 100644 --- a/django/db/models/base.py +++ b/django/db/models/base.py @@ -596,7 +596,7 @@ class Model(six.with_metaclass(ModelBase)): if field.rel and field.get_cache_name() in self.__dict__: rel_instance = getattr(self, field.get_cache_name()) local_val = getattr(db_instance, field.attname) - related_val = getattr(rel_instance, field.related_field.attname) + related_val = None if rel_instance is None else getattr(rel_instance, field.related_field.attname) if local_val != related_val: del self.__dict__[field.get_cache_name()] self._state.db = db_instance._state.db diff --git a/tests/basic/tests.py b/tests/basic/tests.py index d851d858de..934c5587f4 100644 --- a/tests/basic/tests.py +++ b/tests/basic/tests.py @@ -735,6 +735,13 @@ class ModelRefreshTests(TestCase): self.assertFalse(hasattr(s3_copy.selfref, 'touched')) self.assertEqual(s3_copy.selfref, s2) + def test_refresh_null_fk(self): + s1 = SelfRef.objects.create() + s2 = SelfRef.objects.create(selfref=s1) + s2.selfref = None + s2.refresh_from_db() + self.assertEqual(s2.selfref, s1) + def test_refresh_unsaved(self): pub_date = self._truncate_ms(datetime.now()) a = Article.objects.create(pub_date=pub_date)