Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #12667. Added optgroup validation support to model fields with …

…choices. Thanks ramiro.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@12374 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit fbb42c31df691e3f83236ec8d3a25318ab759e87 1 parent a0bae6e
Joseph Kocherhans authored February 02, 2010
16  django/db/models/fields/__init__.py
... ...
@@ -1,16 +1,13 @@
1 1
 import datetime
2 2
 import decimal
3  
-import os
4 3
 import re
5 4
 import time
6 5
 
7 6
 import django.utils.copycompat as copy
8 7
 
9 8
 from django.db import connection
10  
-from django.db.models import signals
11 9
 from django.db.models.fields.subclassing import LegacyConnection
12 10
 from django.db.models.query_utils import QueryWrapper
13  
-from django.dispatch import dispatcher
14 11
 from django.conf import settings
15 12
 from django import forms
16 13
 from django.core import exceptions, validators
@@ -18,7 +15,7 @@
18 15
 from django.utils.functional import curry
19 16
 from django.utils.itercompat import tee
20 17
 from django.utils.text import capfirst
21  
-from django.utils.translation import ugettext_lazy as _, ugettext
  18
+from django.utils.translation import ugettext_lazy as _
22 19
 from django.utils.encoding import smart_unicode, force_unicode, smart_str
23 20
 from django.utils import datetime_safe
24 21
 
@@ -198,8 +195,15 @@ def validate(self, value, model_instance):
198 195
             # Skip validation for non-editable fields.
199 196
             return
200 197
         if self._choices and value:
201  
-            if not value in dict(self.choices):
202  
-                raise exceptions.ValidationError(self.error_messages['invalid_choice'] % value)
  198
+            for option_key, option_value in self.choices:
  199
+                if type(option_value) in (tuple, list):
  200
+                    # This is an optgroup, so look inside the group for options.
  201
+                    for optgroup_key, optgroup_value in option_value:
  202
+                        if value == optgroup_key:
  203
+                            return
  204
+                elif value == option_key:
  205
+                    return
  206
+            raise exceptions.ValidationError(self.error_messages['invalid_choice'] % value)
203 207
 
204 208
         if value is None and not self.null:
205 209
             raise exceptions.ValidationError(self.error_messages['null'])
10  tests/regressiontests/model_fields/tests.py
@@ -173,6 +173,10 @@ def test_charfield_with_choices_raises_error_on_invalid_choice(self):
173 173
         f = models.CharField(choices=[('a','A'), ('b','B')])
174 174
         self.assertRaises(ValidationError, f.clean, "not a", None)
175 175
 
  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
+
176 180
     def test_nullable_integerfield_raises_error_with_blank_false(self):
177 181
         f = models.IntegerField(null=True, blank=False)
178 182
         self.assertRaises(ValidationError, f.clean, None, None)
@@ -202,7 +206,7 @@ def test_boolean_field_doesnt_accept_empty_input(self):
202 206
 class BigIntegerFieldTests(django.test.TestCase):
203 207
     def test_limits(self):
204 208
         # Ensure that values that are right at the limits can be saved
205  
-        # and then retrieved without corruption. 
  209
+        # and then retrieved without corruption.
206 210
         maxval = 9223372036854775807
207 211
         minval = -maxval - 1
208 212
         BigInt.objects.create(value=maxval)
@@ -236,7 +240,7 @@ class TypeCoercionTests(django.test.TestCase):
236 240
     """
237 241
     def test_lookup_integer_in_charfield(self):
238 242
         self.assertEquals(Post.objects.filter(title=9).count(), 0)
239  
-        
  243
+
240 244
     def test_lookup_integer_in_textfield(self):
241 245
         self.assertEquals(Post.objects.filter(body=24).count(), 0)
242  
-        
  246
+

0 notes on commit fbb42c3

Please sign in to comment.
Something went wrong with that request. Please try again.