Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #18153 -- Reverse OneToOne lookups on unsaved instances.

Thanks David Hatch and Anssi Kääriäinen for their inputs.
  • Loading branch information...
commit 3190abcd75b1fcd660353da4001885ef82cbc596 1 parent c9b4e9a
Aymeric Augustin authored October 09, 2012
21  django/db/models/fields/related.py
@@ -261,13 +261,17 @@ def __get__(self, instance, instance_type=None):
@@ -301,8 +305,13 @@ def __set__(self, instance, value):
40  tests/regressiontests/one_to_one_regress/tests.py
@@ -202,3 +202,43 @@ def test_reverse_object_cached_when_related_is_unset(self):
202 202
         with self.assertNumQueries(0):
203 203
             with self.assertRaises(UndergroundBar.DoesNotExist):
204 204
                 self.p1.undergroundbar
  205
+
  206
+    def test_get_reverse_on_unsaved_object(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.assertNumQueries(0):
  217
+            with self.assertRaises(UndergroundBar.DoesNotExist):
  218
+                p.undergroundbar
  219
+
  220
+        UndergroundBar.objects.create()
  221
+
  222
+        # When there's one instance of the origin
  223
+        # (p.undergroundbar used to return that instance)
  224
+        with self.assertNumQueries(0):
  225
+            with self.assertRaises(UndergroundBar.DoesNotExist):
  226
+                p.undergroundbar
  227
+
  228
+        UndergroundBar.objects.create()
  229
+
  230
+        # When there are several instances of the origin
  231
+        with self.assertNumQueries(0):
  232
+            with self.assertRaises(UndergroundBar.DoesNotExist):
  233
+                p.undergroundbar
  234
+
  235
+    def test_set_reverse_on_unsaved_object(self):
  236
+        """
  237
+        Writing to the reverse relation on an unsaved object
  238
+        is impossible too.
  239
+        """
  240
+        p = Place()
  241
+        b = UndergroundBar.objects.create()
  242
+        with self.assertNumQueries(0):
  243
+            with self.assertRaises(ValueError):
  244
+                p.undergroundbar = b

0 notes on commit 3190abc

Please sign in to comment.
Something went wrong with that request. Please try again.