diff --git a/django/db/models/fields/related.py b/django/db/models/fields/related.py
index 157640c..b9b5baf 100644
a
|
b
|
class SingleRelatedObjectDescriptor(object):
|
261 | 261 | try: |
262 | 262 | rel_obj = getattr(instance, self.cache_name) |
263 | 263 | except AttributeError: |
264 | | params = {'%s__pk' % self.related.field.name: instance._get_pk_val()} |
265 | | try: |
266 | | rel_obj = self.get_query_set(instance=instance).get(**params) |
267 | | except self.related.model.DoesNotExist: |
| 264 | related_pk = instance._get_pk_val() |
| 265 | if related_pk is None: |
268 | 266 | rel_obj = None |
269 | 267 | else: |
270 | | setattr(rel_obj, self.related.field.get_cache_name(), instance) |
| 268 | params = {'%s__pk' % self.related.field.name: related_pk} |
| 269 | try: |
| 270 | rel_obj = self.get_query_set(instance=instance).get(**params) |
| 271 | except self.related.model.DoesNotExist: |
| 272 | rel_obj = None |
| 273 | else: |
| 274 | setattr(rel_obj, self.related.field.get_cache_name(), instance) |
271 | 275 | setattr(instance, self.cache_name, rel_obj) |
272 | 276 | if rel_obj is None: |
273 | 277 | raise self.related.model.DoesNotExist |
diff --git a/tests/regressiontests/one_to_one_regress/tests.py b/tests/regressiontests/one_to_one_regress/tests.py
index eced885..ae70ed7 100644
a
|
b
|
class OneToOneRegressionTests(TestCase):
|
202 | 202 | with self.assertNumQueries(0): |
203 | 203 | with self.assertRaises(UndergroundBar.DoesNotExist): |
204 | 204 | self.p1.undergroundbar |
| 205 | |
| 206 | def test_reverse_with_related_unset_and_single_related_object(self): |
| 207 | b = UndergroundBar() |
| 208 | b.save() |
| 209 | p = Place() |
| 210 | with self.assertRaises(UndergroundBar.DoesNotExist): |
| 211 | p.undergroundbar |