Ticket #12667: 12667.diff

File 12667.diff, 3.6 KB (added by Ramiro Morales, 15 years ago)
  • django/db/models/fields/__init__.py

    diff -r bd551af8275b 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 == smart_unicode(k2):
     204                            found = True
     205                            break
     206                else:
     207                    if value == smart_unicode(k):
     208                        found = True
     209                        break
     210            if not found:
    202211                raise exceptions.ValidationError(self.error_messages['invalid_choice'] % value)
    203212
    204213        if value is None and not self.null:
     
    567576
    568577    def get_prep_value(self, value):
    569578        return self.to_python(value)
    570    
     579
    571580    def formfield(self, **kwargs):
    572581        # Passing max_length to forms.CharField means that the value's length
    573582        # will be validated twice. This is considered acceptable since we want
  • tests/regressiontests/model_fields/tests.py

    diff -r bd551af8275b 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.CharField(max_length=1, choices=(('group',(('a','A'),('b','B'))),('c','C')))
     178        self.assertEqual('a', f.clean('a', 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