diff --git a/django/db/models/fields/related.py b/django/db/models/fields/related.py
index a4bc374..e198d35 100644
a
|
b
|
class SingleRelatedObjectDescriptor(six.with_metaclass(RenameRelatedObjectDescri
|
230 | 230 | if not router.allow_relation(value, instance): |
231 | 231 | raise ValueError('Cannot assign "%r": the current database router prevents this relation.' % value) |
232 | 232 | |
233 | | related_pk = tuple([getattr(instance, field.attname) for field in self.related.field.foreign_related_fields]) |
234 | | if None in related_pk: |
235 | | raise ValueError('Cannot assign "%r": "%s" instance isn\'t saved in the database.' % |
236 | | (value, instance._meta.object_name)) |
237 | | |
| 233 | related_pk = tuple([getattr(instance, field.attname) |
| 234 | for field in self.related.field.foreign_related_fields]) |
238 | 235 | # Set the value of the related field to the value of the related object's related field |
239 | 236 | for index, field in enumerate(self.related.field.local_related_fields): |
240 | 237 | setattr(value, field.attname, related_pk[index]) |
diff --git a/tests/one_to_one_regress/tests.py b/tests/one_to_one_regress/tests.py
index 615536b..b10d94e 100644
a
|
b
|
class OneToOneRegressionTests(TestCase):
|
235 | 235 | def test_set_reverse_on_unsaved_object(self): |
236 | 236 | """ |
237 | 237 | Writing to the reverse relation on an unsaved object |
238 | | is impossible too. |
| 238 | is possible. |
239 | 239 | """ |
240 | 240 | p = Place() |
241 | 241 | b = UndergroundBar.objects.create() |
242 | 242 | with self.assertNumQueries(0): |
243 | | with self.assertRaises(ValueError): |
244 | | p.undergroundbar = b |
| 243 | p.undergroundbar = b |
| 244 | self.assertEqual(p.undergroundbar, b) |
| 245 | self.assertEqual(b.place, p) |
| 246 | |
| 247 | p2 = Place() |
| 248 | b2 = UndergroundBar() |
| 249 | with self.assertNumQueries(0): |
| 250 | p2.undergroundbar = b2 |
| 251 | self.assertEqual(p2.undergroundbar, b2) |
| 252 | self.assertEqual(b2.place, p2) |