Fixed #24578 -- Fixed crash with QuerySet.update() on FK to O2O fields.

Thanks Anssi Kääriäinen for review.
This commit is contained in:
Tim Graham 2015-04-07 09:08:05 -04:00
parent 20a98d863f
commit a10b4c010a
3 changed files with 15 additions and 1 deletions

View File

@ -878,7 +878,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.remote_field.field_name)
return getattr(self, field.remote_field.get_related_field().attname)
def clean(self):
"""

View File

@ -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`).

View File

@ -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)