[1.8.x] Fixed #24578 -- Fixed crash with QuerySet.update() on FK to O2O fields.
Thanks Anssi Kääriäinen for review. Backport of 10b4c010ab2cdaa6ba8bfaec3e3540299ea77be from master
This commit is contained in:
parent
d11e87bff5
commit
b1dc128a03
|
@ -912,7 +912,7 @@ class Model(six.with_metaclass(ModelBase)):
|
|||
def prepare_database_save(self, field):
|
||||
if self.pk is None:
|
||||
raise ValueError("Unsaved model instance %r cannot be used in an ORM query." % self)
|
||||
return getattr(self, field.rel.field_name)
|
||||
return getattr(self, field.rel.get_related_field().attname)
|
||||
|
||||
def clean(self):
|
||||
"""
|
||||
|
|
|
@ -28,3 +28,6 @@ Bugfixes
|
|||
|
||||
* Fixed a migration crash when altering
|
||||
:class:`~django.db.models.ManyToManyField`\s (:ticket:`24513`).
|
||||
|
||||
* Fixed a crash with ``QuerySet.update()`` on foreign keys to one-to-one fields
|
||||
(:ticket:`24578`).
|
||||
|
|
|
@ -468,3 +468,14 @@ class OneToOneTests(TestCase):
|
|||
# refs #21563
|
||||
self.assertFalse(hasattr(Director(), 'director'))
|
||||
self.assertFalse(hasattr(School(), 'school'))
|
||||
|
||||
def test_update_one_to_one_pk(self):
|
||||
p1 = Place.objects.create()
|
||||
p2 = Place.objects.create()
|
||||
r1 = Restaurant.objects.create(place=p1)
|
||||
r2 = Restaurant.objects.create(place=p2)
|
||||
w = Waiter.objects.create(restaurant=r1)
|
||||
|
||||
Waiter.objects.update(restaurant=r2)
|
||||
w.refresh_from_db()
|
||||
self.assertEqual(w.restaurant, r2)
|
||||
|
|
Loading…
Reference in New Issue