Ticket #9245: issue9245.diff
File issue9245.diff, 6.5 KB (added by , 13 years ago) |
---|
-
django/trunk/tests/regressiontests/model_fields/tests.py
diff --git a/django/trunk/tests/regressiontests/model_fields/tests.py b/django/trunk/tests/regressiontests/model_fields/tests.py
a b 11 11 from django.utils import unittest 12 12 13 13 from .models import (Foo, Bar, Whiz, BigD, BigS, Image, BigInt, Post, 14 NullBooleanModel, BooleanModel, Document, RenamedField) 14 NullBooleanModel, BooleanModel, Document, RenamedField, 15 CustomIntegerFormField) 15 16 16 17 # If PIL available, do these tests. 17 18 if Image: … … 223 224 self.assertEqual(Whiz(c=None).get_c_display(), None) # Blank value 224 225 self.assertEqual(Whiz(c='').get_c_display(), '') # Empty value 225 226 227 def test_choices_formfield_overrides(self): 228 whiz_meta = Whiz(c=1)._meta 229 field = whiz_meta.get_field_by_name('c')[0] 230 self.assertIs(type(field.formfield()), 231 forms.TypedChoiceField) 232 self.assertIs(type(whiz_meta.get_field_by_name('d')[0].formfield()), 233 CustomIntegerFormField) 234 self.assertIs(type(whiz_meta.get_field_by_name('ts')[0].formfield()), 235 forms.DateTimeField) 236 226 237 class SlugFieldTests(test.TestCase): 227 238 def test_slugfield_max_length(self): 228 239 """ -
django/trunk/tests/regressiontests/model_fields/models.py
diff --git a/django/trunk/tests/regressiontests/model_fields/models.py b/django/trunk/tests/regressiontests/model_fields/models.py
a b 13 13 except ImportError: 14 14 Image = None 15 15 16 from django import forms 16 17 from django.core.files.storage import FileSystemStorage 17 18 from django.db import models 18 19 from django.db.models.fields.files import ImageFieldFile, ImageField … … 29 30 b = models.CharField(max_length=10) 30 31 a = models.ForeignKey(Foo, default=get_foo) 31 32 33 class CustomIntegerFormField(forms.IntegerField): 34 override_for_choices = False 35 36 def __init__(self, *args, **kwargs): 37 if 'choices' in kwargs: 38 del kwargs['choices'] 39 super(CustomIntegerFormField, self).__init__(*args, **kwargs) 40 41 class CustomIntegerField(models.IntegerField): 42 def formfield(self, **kwargs): 43 if 'form_class' not in kwargs: 44 kwargs['form_class'] = CustomIntegerFormField 45 return super(CustomIntegerField, self).formfield(**kwargs) 46 32 47 class Whiz(models.Model): 33 48 CHOICES = ( 34 49 ('Group 1', ( … … 44 59 (0,'Other'), 45 60 ) 46 61 c = models.IntegerField(choices=CHOICES, null=True) 62 d = CustomIntegerField(choices=CHOICES, default=4) 63 ts = models.DateTimeField(auto_now_add=True) 47 64 48 65 class BigD(models.Model): 49 66 d = models.DecimalField(max_digits=38, decimal_places=30) -
django/trunk/django/db/models/fields/__init__.py
diff --git a/django/trunk/django/db/models/fields/__init__.py b/django/trunk/django/db/models/fields/__init__.py
a b 471 471 include_blank = (self.blank or 472 472 not (self.has_default() or 'initial' in kwargs)) 473 473 defaults['choices'] = self.get_choices(include_blank=include_blank) 474 defaults['coerce'] = self.to_python 475 if self.null: 476 defaults['empty_value'] = None 477 form_class = forms.TypedChoiceField 478 # Many of the subclass-specific formfield arguments (min_value, 479 # max_value) don't apply for choice fields, so be sure to only pass 480 # the values that TypedChoiceField will understand. 481 for k in kwargs.keys(): 482 if k not in ('coerce', 'empty_value', 'choices', 'required', 483 'widget', 'label', 'initial', 'help_text', 484 'error_messages', 'show_hidden_initial'): 485 del kwargs[k] 474 if form_class.override_for_choices: 475 defaults['coerce'] = self.to_python 476 if self.null: 477 defaults['empty_value'] = None 478 form_class = forms.TypedChoiceField 479 # Many of the subclass-specific formfield arguments (min_value, 480 # max_value) don't apply for choice fields, so be sure to only 481 # pass the values that TypedChoiceField will understand. 482 for k in kwargs.keys(): 483 if k not in ('coerce', 'empty_value', 'choices', 'required', 484 'widget', 'label', 'initial', 'help_text', 485 'error_messages', 'show_hidden_initial'): 486 del kwargs[k] 486 487 defaults.update(kwargs) 487 488 return form_class(**defaults) 488 489 -
django/trunk/django/forms/fields.py
diff --git a/django/trunk/django/forms/fields.py b/django/trunk/django/forms/fields.py
a b 50 50 'required': _(u'This field is required.'), 51 51 'invalid': _(u'Enter a valid value.'), 52 52 } 53 override_for_choices = False 53 54 54 55 # Tracks each time a Field instance is created. Used to retain order. 55 56 creation_counter = 0 … … 182 183 return result 183 184 184 185 class CharField(Field): 186 override_for_choices = True 187 185 188 def __init__(self, max_length=None, min_length=None, *args, **kwargs): 186 189 self.max_length, self.min_length = max_length, min_length 187 190 super(CharField, self).__init__(*args, **kwargs) … … 209 212 'max_value': _(u'Ensure this value is less than or equal to %(limit_value)s.'), 210 213 'min_value': _(u'Ensure this value is greater than or equal to %(limit_value)s.'), 211 214 } 215 override_for_choices = True 212 216 213 217 def __init__(self, max_value=None, min_value=None, *args, **kwargs): 214 218 self.max_value, self.min_value = max_value, min_value … … 665 669 cleaned to None. 666 670 """ 667 671 widget = NullBooleanSelect 672 override_for_choices = True 668 673 669 674 def to_python(self, value): 670 675 """