diff -r 7f31074118e2 django/db/models/fields/__init__.py
a
|
b
|
|
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 | 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 | 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): |
| 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: |
202 | 212 | raise exceptions.ValidationError(self.error_messages['invalid_choice'] % value) |
203 | 213 | |
204 | 214 | if value is None and not self.null: |
… |
… |
|
567 | 577 | |
568 | 578 | def get_prep_value(self, value): |
569 | 579 | return self.to_python(value) |
570 | | |
| 580 | |
571 | 581 | def formfield(self, **kwargs): |
572 | 582 | # Passing max_length to forms.CharField means that the value's length |
573 | 583 | # will be validated twice. This is considered acceptable since we want |
diff -r 7f31074118e2 tests/regressiontests/model_fields/tests.py
a
|
b
|
|
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 | 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 | 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 | |