              1
          )
          self.assertIs(BusStation._meta.pk.model, BusStation)
          self.assertIs(TrainStation._meta.pk.model, TrainStation)
 
</s> remove     InternalCertificationAudit, BusStation, TrainStation)
 </s> add     InternalCertificationAudit, BusStation, TrainStation, User, Profile) </s> remove             if not self._state.adding and self.pk is not None:
                qs = qs.exclude(pk=self.pk)

 </s> add             # Note that we need to use the pk as defined by model_class, not
            # self.pk. These can be different fields because model inheritance
            # allows single model to have effectively multiple primary keys.
            # Refs #17615.
            model_class_pk = self._get_pk_val(model_class._meta)
            if not self._state.adding and model_class_pk is not None:
                qs = qs.exclude(pk=model_class_pk)
    def test_inherited_unique_field_with_form(self):
        """
        Test that a model which has different primary key for the parent model
        passes unique field checking correctly. Refs #17615.
        """
        class ProfileForm(forms.ModelForm):
            class Meta:
                model = Profile
        User.objects.create(username="user_only")
        p = Profile.objects.create(username="user_with_profile")
        form = ProfileForm({'username': "user_with_profile", 'extra': "hello"},
                           instance=p)
        self.assertTrue(form.is_valid())
              1
          )
          self.assertIs(BusStation._meta.pk.model, BusStation)
          self.assertIs(TrainStation._meta.pk.model, TrainStation)
    def test_inherited_unique_field_with_form(self):
        """
        Test that a model which has different primary key for the parent model
        passes unique field checking correctly. Refs #17615.
        """
        class ProfileForm(forms.ModelForm):
            class Meta:
                model = Profile
        User.objects.create(username="user_only")
        p = Profile.objects.create(username="user_with_profile")
        form = ProfileForm({'username': "user_with_profile", 'extra': "hello"},
                           instance=p)
        self.assertTrue(form.is_valid())
keep keep keep add
 Fixed #17615 -- Corrected unique field validation when using multitable inheritance. The validation used wrong pk value if the parent and child model had different pk fields. Thanks ungenio for the report and patch.


git-svn-id: http://code.djangoproject.com/svn/django/trunk@17920 bcc190cf-cafb-0310-a4f2-bffc1f526a37 
tests/regressiontests/model_inheritance_regress/tests.py