Ticket #14102: 0001-Fixed-14102-Changed-_get_unique_checks-to-respect-th.patch

File 0001-Fixed-14102-Changed-_get_unique_checks-to-respect-th.patch, 3.8 KB (added by Travis Cline, 14 years ago)
  • django/db/models/base.py

    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  
    11import types
    22import sys
    3 import os
    43from itertools import izip
    54import django.db.models.manager     # Imported to register signal handler.
    65from django.core.exceptions import ObjectDoesNotExist, MultipleObjectsReturned, FieldError, ValidationError, NON_FIELD_ERRORS
    from django.db.models.loading import register_models, get_model  
    1615from django.utils.translation import ugettext_lazy as _
    1716import django.utils.copycompat as copy
    1817from django.utils.functional import curry, update_wrapper
    19 from django.utils.encoding import smart_str, force_unicode, smart_unicode
     18from django.utils.encoding import smart_str, force_unicode
    2019from django.utils.text import get_text_list, capfirst
    2120from django.conf import settings
    2221
    class Model(object):  
    744743                    continue
    745744                if f.unique:
    746745                    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:
    748747                    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:
    750749                    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:
    752751                    date_checks.append((model_class, 'month', name, f.unique_for_month))
    753752        return unique_checks, date_checks
    754753
  • tests/modeltests/model_forms/tests.py

    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):  
    156156        form = PostForm({'subtitle': "Finally", "title": "Django 1.0 is released",
    157157            "slug": "Django 1.0", 'posted': '2008-09-03'}, instance=p)
    158158        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.'])
    159163
    160164    def test_inherited_unique_for_date(self):
    161165        p = Post.objects.create(title="Django 1.0 is released",
  • tests/modeltests/validation/test_unique.py

    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):  
    4040            ), m._get_unique_checks()
    4141        )
    4242
     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
    4352class PerformUniqueChecksTest(unittest.TestCase):
    4453    def setUp(self):
    4554        # Set debug to True to gain access to connection.queries.
Back to Top