summaryrefslogtreecommitdiff
path: root/tests/one_to_one
diff options
context:
space:
mode:
authorAnubhav Joshi <anubhav9042@gmail.com>2014-05-19 14:15:55 +0530
committerTim Graham <timograham@gmail.com>2014-06-05 13:12:01 -0400
commit5643a3b51be338196d0b292d5626ad43648448d3 (patch)
tree70651b23bb39652b9ffb869281a77bfaaf9badbb /tests/one_to_one
parent4f72e5f03ae803fd9faa1445245c1050ca004526 (diff)
downloaddjango-5643a3b51be338196d0b292d5626ad43648448d3.tar.gz
Fixed #10811 -- Made assigning unsaved objects to FK, O2O, and GFK raise ValueError.
This prevents silent data loss. Thanks Aymeric Augustin for the initial patch and Loic Bistuer for the review.
Diffstat (limited to 'tests/one_to_one')
-rw-r--r--tests/one_to_one/models.py4
-rw-r--r--tests/one_to_one/tests.py19
2 files changed, 22 insertions, 1 deletions
diff --git a/tests/one_to_one/models.py b/tests/one_to_one/models.py
index b54cabbaa3..f9c0a40ee0 100644
--- a/tests/one_to_one/models.py
+++ b/tests/one_to_one/models.py
@@ -30,6 +30,10 @@ class Restaurant(models.Model):
return "%s the restaurant" % self.place.name
+class Bar(models.Model):
+ place = models.OneToOneField(Place, null=True)
+
+
@python_2_unicode_compatible
class Waiter(models.Model):
restaurant = models.ForeignKey(Restaurant)
diff --git a/tests/one_to_one/tests.py b/tests/one_to_one/tests.py
index d5d955b08e..f0a7a175a7 100644
--- a/tests/one_to_one/tests.py
+++ b/tests/one_to_one/tests.py
@@ -3,7 +3,7 @@ from __future__ import unicode_literals
from django.db import transaction, IntegrityError
from django.test import TestCase
-from .models import (Place, Restaurant, Waiter, ManualPrimaryKey, RelatedModel,
+from .models import (Place, Restaurant, Bar, Waiter, ManualPrimaryKey, RelatedModel,
MultiModel)
@@ -128,3 +128,20 @@ class OneToOneTests(TestCase):
with self.assertRaises(IntegrityError):
with transaction.atomic():
mm.save()
+
+ def test_unsaved_object(self):
+ """
+ #10811 -- Assigning an unsaved object to a OneToOneField
+ should raise an exception.
+ """
+ place = Place(name='User', address='London')
+ with self.assertRaisesMessage(ValueError,
+ 'Cannot assign "%r": "%s" instance isn\'t saved in the database.'
+ % (place, Restaurant.place.field.rel.to._meta.object_name)):
+ Restaurant.objects.create(place=place, serves_hot_dogs=True, serves_pizza=False)
+ bar = Bar()
+ p = Place(name='User', address='London')
+ with self.assertRaisesMessage(ValueError,
+ 'Cannot assign "%r": "%s" instance isn\'t saved in the database.'
+ % (bar, p._meta.object_name)):
+ p.bar = bar