Ticket #22726: one-to-one.patch

File one-to-one.patch, 2.1 KB (added by Vlastimil Zíma, 10 years ago)
  • django/db/models/fields/related.py

    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  
    230230                if not router.allow_relation(value, instance):
    231231                    raise ValueError('Cannot assign "%r": the current database router prevents this relation.' % value)
    232232
    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])
    238235        # Set the value of the related field to the value of the related object's related field
    239236        for index, field in enumerate(self.related.field.local_related_fields):
    240237            setattr(value, field.attname, related_pk[index])
  • tests/one_to_one_regress/tests.py

    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):  
    235235    def test_set_reverse_on_unsaved_object(self):
    236236        """
    237237        Writing to the reverse relation on an unsaved object
    238         is impossible too.
     238        is possible.
    239239        """
    240240        p = Place()
    241241        b = UndergroundBar.objects.create()
    242242        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)
Back to Top