Ticket #18153: 18153.patch

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

    commit e8b169e489ec8c2a45cf67848f119e2be753c2cd
    Author: Aymeric Augustin <aymeric.augustin@m4x.org>
    Date:   Tue Oct 9 14:04:17 2012 +0200
    
        Fixed #18153 -- Reverse OneToOne lookups on unsaved instances.
        
        Thanks David Hatch.
    
    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..6eb0005 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_on_unsaved_object_nullable(self):
     207        """
     208        Regression for #18153 and #19089.
     209
     210        Accessing the reverse relation on an unsaved object
     211        always raises an exception.
     212        """
     213        p = Place()
     214
     215        # When there's no instance of the origin of the one-to-one
     216        with self.assertRaises(UndergroundBar.DoesNotExist):
     217            p.undergroundbar
     218
     219        UndergroundBar.objects.create()
     220
     221        # When there's one instance of the origin
     222        # (p.undergroundbar used to return that instance)
     223        with self.assertRaises(UndergroundBar.DoesNotExist):
     224            p.undergroundbar
     225
     226        UndergroundBar.objects.create()
     227
     228        # When there are several instances of the origin
     229        with self.assertRaises(UndergroundBar.DoesNotExist):
     230            p.undergroundbar
Back to Top