Ticket #12667: 12667.2.diff

File 12667.2.diff, 3.6 KB (added by Ramiro Morales, 15 years ago)

Correct patch. previous fix and test were totally bogus.

  • django/db/models/fields/__init__.py

    diff -r 7f31074118e2 django/db/models/fields/__init__.py
    a b  
    11import datetime
    22import decimal
    3 import os
    43import re
    54import time
    65
    76import django.utils.copycompat as copy
    87
    98from django.db import connection
    10 from django.db.models import signals
    119from django.db.models.fields.subclassing import LegacyConnection
    1210from django.db.models.query_utils import QueryWrapper
    13 from django.dispatch import dispatcher
    1411from django.conf import settings
    1512from django import forms
    1613from django.core import exceptions, validators
     
    1815from django.utils.functional import curry
    1916from django.utils.itercompat import tee
    2017from django.utils.text import capfirst
    21 from django.utils.translation import ugettext_lazy as _, ugettext
     18from django.utils.translation import ugettext_lazy as _
    2219from django.utils.encoding import smart_unicode, force_unicode, smart_str
    2320from django.utils import datetime_safe
    2421
     
    198195            # Skip validation for non-editable fields.
    199196            return
    200197        if self._choices and value:
    201             if not value in dict(self.choices):
     198            found = False
     199            for k, v in self.choices:
     200                if type(v) in (tuple, list):
     201                    # This is an optgroup, so look inside the group for options
     202                    for k2, v2 in v:
     203                        if value == k2:
     204                            found = True
     205                            break
     206                    if found:
     207                        break
     208                elif value == k:
     209                    found = True
     210                    break
     211            if not found:
    202212                raise exceptions.ValidationError(self.error_messages['invalid_choice'] % value)
    203213
    204214        if value is None and not self.null:
     
    567577
    568578    def get_prep_value(self, value):
    569579        return self.to_python(value)
    570    
     580
    571581    def formfield(self, **kwargs):
    572582        # Passing max_length to forms.CharField means that the value's length
    573583        # will be validated twice. This is considered acceptable since we want
  • tests/regressiontests/model_fields/tests.py

    diff -r 7f31074118e2 tests/regressiontests/model_fields/tests.py
    a b  
    173173        f = models.CharField(choices=[('a','A'), ('b','B')])
    174174        self.assertRaises(ValidationError, f.clean, "not a", None)
    175175
     176    def test_choices_validation_supports_named_groups(self):
     177       f = models.IntegerField(choices=(('group',((10,'A'),(20,'B'))),(30,'C')))
     178       self.assertEqual(10, f.clean(10, None))
     179
    176180    def test_nullable_integerfield_raises_error_with_blank_false(self):
    177181        f = models.IntegerField(null=True, blank=False)
    178182        self.assertRaises(ValidationError, f.clean, None, None)
     
    202206class BigIntegerFieldTests(django.test.TestCase):
    203207    def test_limits(self):
    204208        # Ensure that values that are right at the limits can be saved
    205         # and then retrieved without corruption. 
     209        # and then retrieved without corruption.
    206210        maxval = 9223372036854775807
    207211        minval = -maxval - 1
    208212        BigInt.objects.create(value=maxval)
     
    236240    """
    237241    def test_lookup_integer_in_charfield(self):
    238242        self.assertEquals(Post.objects.filter(title=9).count(), 0)
    239        
     243
    240244    def test_lookup_integer_in_textfield(self):
    241245        self.assertEquals(Post.objects.filter(body=24).count(), 0)
    242        
     246
Back to Top