From c16a239e24f04eeae1f6e7c2acbb2c54b5110032 Mon Sep 17 00:00:00 2001
From: Travis Cline <travis.cline@gmail.com>
Date: Thu, 12 Aug 2010 12:48:21 -0500
Subject: [PATCH] Fixed #14102: Changed _get_unique_checks to respect the excludes list for the date field as well.
---
django/db/models/base.py | 9 ++++-----
tests/modeltests/model_forms/tests.py | 4 ++++
tests/modeltests/validation/test_unique.py | 9 +++++++++
3 files changed, 17 insertions(+), 5 deletions(-)
diff --git a/django/db/models/base.py b/django/db/models/base.py
index 07fc501..1927b1e 100644
a
|
b
|
|
1 | 1 | import types |
2 | 2 | import sys |
3 | | import os |
4 | 3 | from itertools import izip |
5 | 4 | import django.db.models.manager # Imported to register signal handler. |
6 | 5 | from django.core.exceptions import ObjectDoesNotExist, MultipleObjectsReturned, FieldError, ValidationError, NON_FIELD_ERRORS |
… |
… |
from django.db.models.loading import register_models, get_model
|
16 | 15 | from django.utils.translation import ugettext_lazy as _ |
17 | 16 | import django.utils.copycompat as copy |
18 | 17 | from django.utils.functional import curry, update_wrapper |
19 | | from django.utils.encoding import smart_str, force_unicode, smart_unicode |
| 18 | from django.utils.encoding import smart_str, force_unicode |
20 | 19 | from django.utils.text import get_text_list, capfirst |
21 | 20 | from django.conf import settings |
22 | 21 | |
… |
… |
class Model(object):
|
744 | 743 | continue |
745 | 744 | if f.unique: |
746 | 745 | unique_checks.append((model_class, (name,))) |
747 | | if f.unique_for_date: |
| 746 | if f.unique_for_date and f.unique_for_date not in exclude: |
748 | 747 | date_checks.append((model_class, 'date', name, f.unique_for_date)) |
749 | | if f.unique_for_year: |
| 748 | if f.unique_for_year and f.unique_for_year not in exclude: |
750 | 749 | date_checks.append((model_class, 'year', name, f.unique_for_year)) |
751 | | if f.unique_for_month: |
| 750 | if f.unique_for_month and f.unique_for_month not in exclude: |
752 | 751 | date_checks.append((model_class, 'month', name, f.unique_for_month)) |
753 | 752 | return unique_checks, date_checks |
754 | 753 | |
diff --git a/tests/modeltests/model_forms/tests.py b/tests/modeltests/model_forms/tests.py
index 6a5f939..c5647c7 100644
a
|
b
|
class UniqueTest(TestCase):
|
156 | 156 | form = PostForm({'subtitle': "Finally", "title": "Django 1.0 is released", |
157 | 157 | "slug": "Django 1.0", 'posted': '2008-09-03'}, instance=p) |
158 | 158 | self.assertTrue(form.is_valid()) |
| 159 | form = PostForm({'title': "Django 1.0 is released"}) |
| 160 | self.assertFalse(form.is_valid()) |
| 161 | self.assertEqual(len(form.errors), 1) |
| 162 | self.assertEqual(form.errors['posted'], [u'This field is required.']) |
159 | 163 | |
160 | 164 | def test_inherited_unique_for_date(self): |
161 | 165 | p = Post.objects.create(title="Django 1.0 is released", |
diff --git a/tests/modeltests/validation/test_unique.py b/tests/modeltests/validation/test_unique.py
index 1b96639..fb77c4d 100644
a
|
b
|
class GetUniqueCheckTests(unittest.TestCase):
|
40 | 40 | ), m._get_unique_checks() |
41 | 41 | ) |
42 | 42 | |
| 43 | def test_unique_for_date_exclusion(self): |
| 44 | m = UniqueForDateModel() |
| 45 | self.assertEqual(( |
| 46 | [(UniqueForDateModel, ('id',))], |
| 47 | [(UniqueForDateModel, 'year', 'count', 'end_date'), |
| 48 | (UniqueForDateModel, 'month', 'order', 'end_date')] |
| 49 | ), m._get_unique_checks(exclude='start_date') |
| 50 | ) |
| 51 | |
43 | 52 | class PerformUniqueChecksTest(unittest.TestCase): |
44 | 53 | def setUp(self): |
45 | 54 | # Set debug to True to gain access to connection.queries. |