Ticket #17615: unique_field_with_mti.diff

File unique_field_with_mti.diff, 3.6 KB (added by David Bennett, 13 years ago)

Test and fix.

  • django/db/models/base.py

    From 0448e5c6f1217dc35c89493f0a5baa28572a301e Mon Sep 17 00:00:00 2001
    From: David Bennett <david@dbinit.com>
    Date: Mon, 30 Jan 2012 01:00:52 -0600
    Subject: [PATCH] Fix for unique validation on parent model with multi-table
     inheritance.
    
    ---
     django/db/models/base.py |    6 +++++-
     1 files changed, 5 insertions(+), 1 deletions(-)
    
    diff --git a/django/db/models/base.py b/django/db/models/base.py
    index 286f9b0..8d4dc02 100644
    a b class Model(object):  
    724724            # Exclude the current object from the query if we are editing an
    725725            # instance (as opposed to creating a new one)
    726726            if not self._state.adding and self.pk is not None:
    727                 qs = qs.exclude(pk=self.pk)
     727                if model_class is self.__class__:
     728                    pk = self.pk
     729                else:
     730                    pk = getattr(self, self._meta.get_ancestor_link(model_class).attname)
     731                qs = qs.exclude(pk=pk)
    728732
    729733            if qs.exists():
    730734                if len(unique_check) == 1:
  • tests/regressiontests/model_inheritance_regress/models.py

    -- 
    1.7.7.3
    
    From e755862a10697768ffa0581d5679d2fa433f6b62 Mon Sep 17 00:00:00 2001
    From: David Bennett <david@dbinit.com>
    Date: Mon, 30 Jan 2012 14:19:10 -0600
    Subject: [PATCH] Added test.
    
    ---
     .../model_inheritance_regress/models.py            |    7 +++++++
     .../model_inheritance_regress/tests.py             |   14 +++++++++++++-
     2 files changed, 20 insertions(+), 1 deletions(-)
    
    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 3a74360..4e810fe 100644
    a b Regression tests for Model inheritance behaviour.  
    44
    55import datetime
    66from operator import attrgetter
     7from django import forms
    78
    89from django.test import TestCase
    910
    from models import (Place, Restaurant, ItalianRestaurant, ParkingLot,  
    1112    ParkingLot2, ParkingLot3, Supplier, Wholesaler, Child, SelfRefParent,
    1213    SelfRefChild, ArticleWithAuthor, M2MChild, QualityControl, DerivedM,
    1314    Person, BirthdayParty, BachelorParty, MessyBachelorParty,
    14     InternalCertificationAudit, BusStation, TrainStation)
     15    InternalCertificationAudit, BusStation, TrainStation, User, Profile)
     16
     17
     18class ProfileForm(forms.ModelForm):
     19    class Meta:
     20        model = Profile
    1521
    1622
    1723class ModelInheritanceTest(TestCase):
    class ModelInheritanceTest(TestCase):  
    406412        )
    407413        self.assertIs(BusStation._meta.pk.model, BusStation)
    408414        self.assertIs(TrainStation._meta.pk.model, TrainStation)
     415
     416    def test_inherited_unique_field_with_form(self):
     417        User.objects.create(username="user_only")
     418        p = Profile.objects.create(username="user_with_profile")
     419        form = ProfileForm({'username': "user_with_profile", 'extra': "hello"}, instance=p)
     420        self.assertTrue(form.is_valid())
Back to Top