Ticket #15321: sub_sub_unique_fix.patch

File sub_sub_unique_fix.patch, 4.1 KB (added by sswang, 14 years ago)
  • django/db/models/base.py

     
    677677        # Gather a list of checks for fields declared as unique and add them to
    678678        # the list of checks.
    679679
    680         fields_with_class = [(self.__class__, self._meta.local_fields)]
    681         for parent_class in self._meta.parents.keys():
    682             fields_with_class.append((parent_class, parent_class._meta.local_fields))
     680        fields_with_class = []
     681        for field in self._meta.fields:
     682            name, model_class, junk, junk = self._meta.get_field_by_name(field.name)
     683            if model_class == None:
     684                model_class = self.__class__
     685            fields_with_class.append((model_class, field))
    683686
    684         for model_class, fields in fields_with_class:
    685             for f in fields:
    686                 name = f.name
    687                 if name in exclude:
    688                     continue
    689                 if f.unique:
    690                     unique_checks.append((model_class, (name,)))
    691                 if f.unique_for_date and f.unique_for_date not in exclude:
    692                     date_checks.append((model_class, 'date', name, f.unique_for_date))
    693                 if f.unique_for_year and f.unique_for_year not in exclude:
    694                     date_checks.append((model_class, 'year', name, f.unique_for_year))
    695                 if f.unique_for_month and f.unique_for_month not in exclude:
    696                     date_checks.append((model_class, 'month', name, f.unique_for_month))
     687        for model_class, f in fields_with_class:
     688            name = f.name
     689            if name in exclude:
     690                continue
     691            if f.unique:
     692                unique_checks.append((model_class, (name,)))
     693            if f.unique_for_date and f.unique_for_date not in exclude:
     694                date_checks.append((model_class, 'date', name, f.unique_for_date))
     695            if f.unique_for_year and f.unique_for_year not in exclude:
     696                date_checks.append((model_class, 'year', name, f.unique_for_year))
     697            if f.unique_for_month and f.unique_for_month not in exclude:
     698                date_checks.append((model_class, 'month', name, f.unique_for_month))
    697699        return unique_checks, date_checks
    698700
    699701    def _perform_unique_checks(self, unique_checks):
  • tests/modeltests/validation/models.py

     
    2727    unique_integerfield = models.IntegerField(unique=True)
    2828    non_unique_field = models.IntegerField()
    2929
     30class SubUniqueFieldsModel(UniqueFieldsModel):
     31    pass
     32
     33class SubSubUniqueFieldsModel(SubUniqueFieldsModel):
     34    pass
     35
    3036class CustomPKModel(models.Model):
    3137    my_pk_field = models.CharField(max_length=100, primary_key=True)
    3238
  • tests/modeltests/validation/test_unique.py

     
    77from django.utils import unittest
    88
    99from models import (CustomPKModel, UniqueTogetherModel, UniqueFieldsModel,
    10     UniqueForDateModel, ModelToValidate, Post, FlexibleDatePost)
     10    UniqueForDateModel, ModelToValidate, Post, FlexibleDatePost,
     11    SubSubUniqueFieldsModel)
    1112
    1213
    1314class GetUniqueCheckTests(unittest.TestCase):
     
    149150            self.fail("unique_for_month checks shouldn't trigger when the associated DateField is None.")
    150151        except:
    151152            self.fail("unique_for_month checks shouldn't explode when the associated DateField is None.")
     153
     154    def test_sub_sub_unique_check(self):
     155        unique_sub_sub = SubSubUniqueFieldsModel.objects.create(unique_charfield="unique_charfield", unique_integerfield=1, non_unique_field=2)
     156        unique_sub_sub2 = SubSubUniqueFieldsModel(unique_charfield="unique_charfield", unique_integerfield=1, non_unique_field=2)
     157        self.assertRaises(ValidationError, unique_sub_sub2.full_clean)
     158
Back to Top