diff --git a/django/db/models/base.py b/django/db/models/base.py
index ebd67be..d919648 100644
a
|
b
|
class Model(object):
|
719 | 719 | |
720 | 720 | # Exclude the current object from the query if we are editing an |
721 | 721 | # instance (as opposed to creating a new one) |
722 | | if not self._state.adding and self.pk is not None: |
723 | | qs = qs.exclude(pk=self.pk) |
724 | | |
| 722 | # Note that we need to use the pk as defined by model_class, not |
| 723 | # self.pk. These can be different fields because model inheritance |
| 724 | # allows single model to have effectively multiple primary keys. |
| 725 | # Refs #17615. |
| 726 | model_class_pk = self._get_pk_val(model_class._meta) |
| 727 | if not self._state.adding and model_class_pk is not None: |
| 728 | qs = qs.exclude(pk=model_class_pk) |
725 | 729 | if qs.exists(): |
726 | 730 | if len(unique_check) == 1: |
727 | 731 | key = unique_check[0] |
diff --git a/tests/regressiontests/model_inheritance_regress/models.py b/tests/regressiontests/model_inheritance_regress/models.py
index bb5d77c..161569b 100644
a
|
b
|
class BusStation(Station):
|
163 | 163 | |
164 | 164 | class TrainStation(Station): |
165 | 165 | zone = models.IntegerField() |
| 166 | |
| 167 | class User(models.Model): |
| 168 | username = models.CharField(max_length=30, unique=True) |
| 169 | |
| 170 | class Profile(User): |
| 171 | profile_id = models.AutoField(primary_key=True) |
| 172 | extra = models.CharField(max_length=30, blank=True) |
diff --git a/tests/regressiontests/model_inheritance_regress/tests.py b/tests/regressiontests/model_inheritance_regress/tests.py
index 8e2c56b..43f1b7b 100644
a
|
b
|
from __future__ import absolute_import
|
6 | 6 | |
7 | 7 | import datetime |
8 | 8 | from operator import attrgetter |
| 9 | from django import forms |
9 | 10 | |
10 | 11 | from django.test import TestCase |
11 | 12 | |
… |
… |
from .models import (Place, Restaurant, ItalianRestaurant, ParkingLot,
|
13 | 14 | ParkingLot2, ParkingLot3, Supplier, Wholesaler, Child, SelfRefParent, |
14 | 15 | SelfRefChild, ArticleWithAuthor, M2MChild, QualityControl, DerivedM, |
15 | 16 | Person, BirthdayParty, BachelorParty, MessyBachelorParty, |
16 | | InternalCertificationAudit, BusStation, TrainStation) |
| 17 | InternalCertificationAudit, BusStation, TrainStation, User, Profile) |
| 18 | |
| 19 | |
| 20 | class ProfileForm(forms.ModelForm): |
| 21 | class Meta: |
| 22 | model = Profile |
17 | 23 | |
18 | 24 | |
19 | 25 | class ModelInheritanceTest(TestCase): |
… |
… |
class ModelInheritanceTest(TestCase):
|
408 | 414 | ) |
409 | 415 | self.assertIs(BusStation._meta.pk.model, BusStation) |
410 | 416 | self.assertIs(TrainStation._meta.pk.model, TrainStation) |
| 417 | |
| 418 | def test_inherited_unique_field_with_form(self): |
| 419 | User.objects.create(username="user_only") |
| 420 | p = Profile.objects.create(username="user_with_profile") |
| 421 | form = ProfileForm({'username': "user_with_profile", 'extra': "hello"}, instance=p) |
| 422 | self.assertTrue(form.is_valid()) |