diff --git a/django/db/models/fields/related.py b/django/db/models/fields/related.py
index 4caed4f..937e9d3 100644
a
|
b
|
class ReverseSingleRelatedObjectDescriptor(six.with_metaclass(RenameRelatedObjec
|
331 | 331 | if not router.allow_relation(value, instance): |
332 | 332 | raise ValueError('Cannot assign "%r": the current database router prevents this relation.' % value) |
333 | 333 | |
| 334 | pk = tuple(getattr(value, field.attname, None) for _, field in self.field.related_fields) |
| 335 | if None in pk and self.field.null is False: |
| 336 | raise ValueError('Cannot assign "%r": "%s" instance isn\'t saved in the database.' % |
| 337 | (value, self.field.rel.to._meta.object_name)) |
| 338 | |
334 | 339 | # If we're setting the value of a OneToOneField to None, we need to clear |
335 | 340 | # out the cache on any old related object. Otherwise, deleting the |
336 | 341 | # previously-related object will also cause this object to be deleted, |
… |
… |
class ReverseSingleRelatedObjectDescriptor(six.with_metaclass(RenameRelatedObjec
|
352 | 357 | |
353 | 358 | # Set the value of the related field |
354 | 359 | for lh_field, rh_field in self.field.related_fields: |
355 | | try: |
356 | | setattr(instance, lh_field.attname, getattr(value, rh_field.attname)) |
357 | | except AttributeError: |
358 | | setattr(instance, lh_field.attname, None) |
| 360 | setattr(instance, lh_field.attname, getattr(value, rh_field.attname, None)) |
359 | 361 | |
360 | 362 | # Since we already know what the related object is, seed the related |
361 | 363 | # object caches now, too. This avoids another db hit if you get the |
diff --git a/tests/admin_util/tests.py b/tests/admin_util/tests.py
index 8c63e90..859967b 100644
a
|
b
|
class UtilTests(SimpleTestCase):
|
100 | 100 | simple_function = lambda obj: SIMPLE_FUNCTION |
101 | 101 | |
102 | 102 | article = Article( |
103 | | site=Site(domain=SITE_NAME), |
| 103 | site=Site.objects.create(domain=SITE_NAME), |
104 | 104 | title=TITLE_TEXT, |
105 | 105 | created=CREATED_DATE, |
106 | 106 | ) |
diff --git a/tests/many_to_one_regress/tests.py b/tests/many_to_one_regress/tests.py
index 19cfaad..7ad8590 100644
a
|
b
|
class ManyToOneRegressionTests(TestCase):
|
65 | 65 | c = Child(parent=p) |
66 | 66 | self.assertTrue(c.parent is p) |
67 | 67 | |
68 | | # Creation using keyword argument and unsaved related instance (#8070). |
69 | | p = Parent() |
70 | | c = Child(parent=p) |
71 | | self.assertTrue(c.parent is p) |
72 | | |
73 | 68 | # Creation using attname keyword argument and an id will cause the |
74 | 69 | # related object to be fetched. |
75 | 70 | p = Parent.objects.get(name="Parent") |
diff --git a/tests/model_formsets/tests.py b/tests/model_formsets/tests.py
index 0534481..dc4258e 100644
a
|
b
|
class ModelFormsetTest(TestCase):
|
548 | 548 | 'book_set-2-title': '', |
549 | 549 | } |
550 | 550 | |
551 | | formset = AuthorBooksFormSet(data, instance=Author(), save_as_new=True) |
| 551 | new_author = Author.objects.create(name='Charles Baudelaire') |
| 552 | |
| 553 | formset = AuthorBooksFormSet(data, instance=new_author, save_as_new=True) |
552 | 554 | self.assertTrue(formset.is_valid()) |
553 | 555 | |
554 | | new_author = Author.objects.create(name='Charles Baudelaire') |
555 | 556 | formset = AuthorBooksFormSet(data, instance=new_author, save_as_new=True) |
556 | 557 | saved = formset.save() |
557 | 558 | self.assertEqual(len(saved), 2) |
diff --git a/tests/model_regress/tests.py b/tests/model_regress/tests.py
index f84a40b..c1f1b97 100644
a
|
b
|
import sys
|
6 | 6 | import unittest |
7 | 7 | |
8 | 8 | from django.core.exceptions import ValidationError |
9 | | from django.test import TestCase, skipUnlessDBFeature |
10 | | from django.utils import six |
11 | | from django.utils import tzinfo |
12 | 9 | from django.db import connection, router |
13 | 10 | from django.db.models.sql import InsertQuery |
| 11 | from django.test import TestCase, skipUnlessDBFeature |
| 12 | from django.utils import six, tzinfo |
14 | 13 | |
15 | 14 | from .models import (Worker, Article, Party, Event, Department, |
16 | 15 | BrokenUnicodeMethod, NonAutoPK, Model1, Model2, Model3) |
… |
… |
class ModelTests(TestCase):
|
214 | 213 | m3 = Model3.objects.get(model2=1000) |
215 | 214 | m3.model2 |
216 | 215 | |
| 216 | def test_assign_unsaved_instance_to_fk(self): |
| 217 | """ |
| 218 | Regression for #10811: Can't assign an unsaved instance to a FK |
| 219 | """ |
| 220 | w = Worker(name="Joe") |
| 221 | d = Department(name="Accounting") # not saved in the database |
| 222 | with six.assertRaisesRegex(self, ValueError, |
| 223 | 'Cannot assign "<Department: Accounting>": ' |
| 224 | '"Department" instance isn\'t saved in the database.'): |
| 225 | w.department = d |
| 226 | |
217 | 227 | |
218 | 228 | class ModelValidationTest(TestCase): |
219 | 229 | def test_pk_validation(self): |
diff --git a/tests/one_to_one_regress/tests.py b/tests/one_to_one_regress/tests.py
index 55836d4..07a205c 100644
a
|
b
|
class OneToOneRegressionTests(TestCase):
|
94 | 94 | r = Restaurant(place=p) |
95 | 95 | self.assertTrue(r.place is p) |
96 | 96 | |
97 | | # Creation using keyword argument and unsaved related instance (#8070). |
98 | | p = Place() |
99 | | r = Restaurant(place=p) |
100 | | self.assertTrue(r.place is p) |
101 | | |
102 | 97 | # Creation using attname keyword argument and an id will cause the related |
103 | 98 | # object to be fetched. |
104 | 99 | p = Place.objects.get(name="Demon Dogs") |