diff --git a/django/db/models/base.py b/django/db/models/base.py
index 1927b1e..0ac34e4 100644
a
|
b
|
class Model(object):
|
399 | 399 | return getattr(self, meta.pk.attname) |
400 | 400 | |
401 | 401 | def _set_pk_val(self, value): |
402 | | return setattr(self, self._meta.pk.attname, value) |
| 402 | ret = setattr(self, self._meta.pk.attname, value) |
| 403 | parent_link = self._meta.pk |
| 404 | while (isinstance(parent_link, OneToOneField) and parent_link.rel.parent_link): |
| 405 | parent_link = parent_link.rel.get_related_field() |
| 406 | setattr(self, parent_link.attname, value) |
| 407 | return ret |
403 | 408 | |
404 | 409 | pk = property(_get_pk_val, _set_pk_val) |
405 | 410 | |
diff --git a/tests/modeltests/model_inheritance/models.py b/tests/modeltests/model_inheritance/models.py
index 95bf5ab..5a6d7c2 100644
a
|
b
|
class ParkingLot(Place):
|
119 | 119 | def __unicode__(self): |
120 | 120 | return u"%s the parking lot" % self.name |
121 | 121 | |
| 122 | class CarInLot(models.Model): |
| 123 | parking_lot = models.ForeignKey(ParkingLot) |
| 124 | |
122 | 125 | # |
123 | 126 | # Abstract base classes with related models where the sub-class has the |
124 | 127 | # same name in a different app and inherits from the same abstract base |
… |
… |
DoesNotExist: Restaurant matching query does not exist.
|
345 | 348 | >>> Restaurant.objects.get(lot__name='Well Lit') |
346 | 349 | <Restaurant: Ristorante Miron the restaurant> |
347 | 350 | |
| 351 | # Setting the pk of a model with OneToOneField, where parent_link=True, will |
| 352 | # also set the parent model's pk. |
| 353 | |
| 354 | >>> ir2 = ItalianRestaurant(pk=1) |
| 355 | >>> ir2.id == ir2.pk |
| 356 | True |
| 357 | >>> ir2.pk = 2 |
| 358 | >>> ir2.pk == ir2.id |
| 359 | True |
| 360 | |
| 361 | >>> park3 = ParkingLot(pk=1) |
| 362 | >>> park3.pk == park3.id |
| 363 | True |
| 364 | >>> park3.parent_id == park3.pk |
| 365 | True |
| 366 | >>> cil = CarInLot(parking_lot=park3) |
| 367 | >>> cil.parking_lot_id == 1 |
| 368 | True |
| 369 | |
348 | 370 | # The update() command can update fields in parent and child classes at once |
349 | 371 | # (although it executed multiple SQL queries to do so). |
350 | 372 | >>> Restaurant.objects.filter(serves_hot_dogs=True, name__contains='D').update(name='Demon Puppies', serves_hot_dogs=False) |