Ticket #14368: 14368.patch
File 14368.patch, 3.4 KB (added by , 14 years ago) |
---|
-
django/db/models/fields/related.py
254 254 raise ValueError('Cannot assign "%r": instance is on database "%s", value is on database "%s"' % 255 255 (value, instance._state.db, value._state.db)) 256 256 257 # Set the value of the related field to the value of the related object's related field 258 setattr(value, self.related.field.attname, getattr(instance, self.related.field.rel.get_related_field().attname)) 257 if value is not None: 258 # Set the value of the related field to the value of the related object's related field 259 setattr(value, self.related.field.attname, getattr(instance, self.related.field.rel.get_related_field().attname)) 260 261 # Since we already know what the related object is, seed the related 262 # object caches now, too. This avoids another db hit if you get the 263 # object you just set. 264 setattr(instance, self.cache_name, value) 265 setattr(value, self.related.field.get_cache_name(), instance) 266 else: 267 value = getattr(instance, self.related.var_name, None) 268 if value is not None: 269 setattr(value, self.related.field.name, None) 270 try: 271 delattr(instance, self.cache_name) 272 except AttributeError: 273 pass 259 274 260 # Since we already know what the related object is, seed the related261 # object caches now, too. This avoids another db hit if you get the262 # object you just set.263 setattr(instance, self.cache_name, value)264 setattr(value, self.related.field.get_cache_name(), instance)265 266 275 class ReverseSingleRelatedObjectDescriptor(object): 267 276 # This class provides the functionality that makes the related-object 268 277 # managers available as attributes on a model class, for fields that have -
tests/modeltests/one_to_one/tests.py
1 from django.test import TestCase 2 3 from models import First, Second 4 5 class OneToOneTestCase(TestCase): 6 7 def test_set_related_to_none(self): 8 # regression test for #14368 9 first = First.objects.create() 10 second = Second.objects.create() 11 first.second = second 12 first.second = None 13 self.assertRaises(Second.DoesNotExist, getattr, first, 'second') 14 self.assert_(second.first is None) -
tests/modeltests/one_to_one/models.py
46 46 def __unicode__(self): 47 47 return u"Multimodel %s" % self.name 48 48 49 class First(models.Model): 50 pass 51 52 class Second(models.Model): 53 first = models.OneToOneField(First, null=True) 54 49 55 __test__ = {'API_TESTS':""" 50 56 # Create a couple of Places. 51 57 >>> p1 = Place(name='Demon Dogs', address='944 W. Fullerton') … … 190 196 ... print "Fail with %s" % type(e) 191 197 Pass 192 198 >>> transaction.savepoint_rollback(sid) 193 194 199 """}