Ticket #15321: sub_sub_unique_fix.patch
File sub_sub_unique_fix.patch, 4.1 KB (added by , 14 years ago) |
---|
-
django/db/models/base.py
677 677 # Gather a list of checks for fields declared as unique and add them to 678 678 # the list of checks. 679 679 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)) 683 686 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)) 697 699 return unique_checks, date_checks 698 700 699 701 def _perform_unique_checks(self, unique_checks): -
tests/modeltests/validation/models.py
27 27 unique_integerfield = models.IntegerField(unique=True) 28 28 non_unique_field = models.IntegerField() 29 29 30 class SubUniqueFieldsModel(UniqueFieldsModel): 31 pass 32 33 class SubSubUniqueFieldsModel(SubUniqueFieldsModel): 34 pass 35 30 36 class CustomPKModel(models.Model): 31 37 my_pk_field = models.CharField(max_length=100, primary_key=True) 32 38 -
tests/modeltests/validation/test_unique.py
7 7 from django.utils import unittest 8 8 9 9 from models import (CustomPKModel, UniqueTogetherModel, UniqueFieldsModel, 10 UniqueForDateModel, ModelToValidate, Post, FlexibleDatePost) 10 UniqueForDateModel, ModelToValidate, Post, FlexibleDatePost, 11 SubSubUniqueFieldsModel) 11 12 12 13 13 14 class GetUniqueCheckTests(unittest.TestCase): … … 149 150 self.fail("unique_for_month checks shouldn't trigger when the associated DateField is None.") 150 151 except: 151 152 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