Ticket #19089: 19089.patch

File 19089.patch, 2.0 KB (added by Aymeric Augustin, 12 years ago)
  • django/db/models/fields/related.py

    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):  
    261261        try:
    262262            rel_obj = getattr(instance, self.cache_name)
    263263        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:
    268266                rel_obj = None
    269267            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)
    271275            setattr(instance, self.cache_name, rel_obj)
    272276        if rel_obj is None:
    273277            raise self.related.model.DoesNotExist
  • tests/regressiontests/one_to_one_regress/tests.py

    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):  
    202202        with self.assertNumQueries(0):
    203203            with self.assertRaises(UndergroundBar.DoesNotExist):
    204204                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
Back to Top