Ticket #17615: 17615.diff

File 17615.diff, 3.1 KB (added by akaariai, 4 years ago)

A minor cleanup to unique_field_with_mti.diff

  • django/db/models/base.py

    diff --git a/django/db/models/base.py b/django/db/models/base.py
    index ebd67be..d919648 100644
    a b class Model(object): 
    719719
    720720            # Exclude the current object from the query if we are editing an
    721721            # 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)
    725729            if qs.exists():
    726730                if len(unique_check) == 1:
    727731                    key = unique_check[0]
  • tests/regressiontests/model_inheritance_regress/models.py

    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): 
    163163
    164164class TrainStation(Station):
    165165    zone = models.IntegerField()
     166
     167class User(models.Model):
     168    username = models.CharField(max_length=30, unique=True)
     169
     170class Profile(User):
     171    profile_id = models.AutoField(primary_key=True)
     172    extra = models.CharField(max_length=30, blank=True)
  • tests/regressiontests/model_inheritance_regress/tests.py

    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 
    66
    77import datetime
    88from operator import attrgetter
     9from django import forms
    910
    1011from django.test import TestCase
    1112
    from .models import (Place, Restaurant, ItalianRestaurant, ParkingLot, 
    1314    ParkingLot2, ParkingLot3, Supplier, Wholesaler, Child, SelfRefParent,
    1415    SelfRefChild, ArticleWithAuthor, M2MChild, QualityControl, DerivedM,
    1516    Person, BirthdayParty, BachelorParty, MessyBachelorParty,
    16     InternalCertificationAudit, BusStation, TrainStation)
     17    InternalCertificationAudit, BusStation, TrainStation, User, Profile)
     18
     19
     20class ProfileForm(forms.ModelForm):
     21    class Meta:
     22        model = Profile
    1723
    1824
    1925class ModelInheritanceTest(TestCase):
    class ModelInheritanceTest(TestCase): 
    408414        )
    409415        self.assertIs(BusStation._meta.pk.model, BusStation)
    410416        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())
Back to Top