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):
|
| 724 | 724 | # Exclude the current object from the query if we are editing an |
| 725 | 725 | # instance (as opposed to creating a new one) |
| 726 | 726 | 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) |
| 728 | 732 | |
| 729 | 733 | if qs.exists(): |
| 730 | 734 | if len(unique_check) == 1: |
--
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):
|
| 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 3a74360..4e810fe 100644
|
a
|
b
|
Regression tests for Model inheritance behaviour.
|
| 4 | 4 | |
| 5 | 5 | import datetime |
| 6 | 6 | from operator import attrgetter |
| | 7 | from django import forms |
| 7 | 8 | |
| 8 | 9 | from django.test import TestCase |
| 9 | 10 | |
| … |
… |
from models import (Place, Restaurant, ItalianRestaurant, ParkingLot,
|
| 11 | 12 | ParkingLot2, ParkingLot3, Supplier, Wholesaler, Child, SelfRefParent, |
| 12 | 13 | SelfRefChild, ArticleWithAuthor, M2MChild, QualityControl, DerivedM, |
| 13 | 14 | Person, BirthdayParty, BachelorParty, MessyBachelorParty, |
| 14 | | InternalCertificationAudit, BusStation, TrainStation) |
| | 15 | InternalCertificationAudit, BusStation, TrainStation, User, Profile) |
| | 16 | |
| | 17 | |
| | 18 | class ProfileForm(forms.ModelForm): |
| | 19 | class Meta: |
| | 20 | model = Profile |
| 15 | 21 | |
| 16 | 22 | |
| 17 | 23 | class ModelInheritanceTest(TestCase): |
| … |
… |
class ModelInheritanceTest(TestCase):
|
| 406 | 412 | ) |
| 407 | 413 | self.assertIs(BusStation._meta.pk.model, BusStation) |
| 408 | 414 | 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()) |