Ticket #9245: issue9245-alt.diff
File issue9245-alt.diff, 10.1 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 def __init__(self, *args, **kwargs): 35 if 'choices' in kwargs: 36 del kwargs['choices'] 37 super(CustomIntegerFormField, self).__init__(*args, **kwargs) 38 39 class CustomIntegerField(models.IntegerField): 40 def formfield(self, **kwargs): 41 if 'form_class' not in kwargs: 42 kwargs['form_class'] = CustomIntegerFormField 43 return super(CustomIntegerField, self).formfield(**kwargs) 44 32 45 class Whiz(models.Model): 33 46 CHOICES = ( 34 47 ('Group 1', ( … … 44 57 (0,'Other'), 45 58 ) 46 59 c = models.IntegerField(choices=CHOICES, null=True) 60 d = CustomIntegerField(choices=CHOICES, default=4) 61 ts = models.DateTimeField(auto_now_add=True) 47 62 48 63 class BigD(models.Model): 49 64 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 453 453 def save_form_data(self, instance, data): 454 454 setattr(instance, self.name, data) 455 455 456 def formfield(self, form_class= forms.CharField, **kwargs):456 def formfield(self, form_class=None, **kwargs): 457 457 """ 458 458 Returns a django.forms.Field instance for this database Field. 459 459 """ … … 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 not form_class: 475 form_class = forms.TypedChoiceField 476 defaults['coerce'] = self.to_python 477 if self.null: 478 defaults['empty_value'] = None 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] 487 if not form_class: 488 form_class = forms.CharField 486 489 defaults.update(kwargs) 487 490 return form_class(**defaults) 488 491 … … 722 725 return '' if val is None else val.isoformat() 723 726 724 727 def formfield(self, **kwargs): 725 defaults = {'form_class': forms.DateField} 726 defaults.update(kwargs) 728 if self.choices: 729 defaults = kwargs 730 else: 731 defaults = {'form_class': forms.DateField} 732 defaults.update(kwargs) 727 733 return super(DateField, self).formfield(**defaults) 728 734 729 735 class DateTimeField(DateField): … … 821 827 return '' if val is None else val.isoformat() 822 828 823 829 def formfield(self, **kwargs): 824 defaults = {'form_class': forms.DateTimeField} 825 defaults.update(kwargs) 830 if self.choices: 831 defaults = kwargs 832 else: 833 defaults = {'form_class': forms.DateTimeField} 834 defaults.update(kwargs) 826 835 return super(DateTimeField, self).formfield(**defaults) 827 836 828 837 class DecimalField(Field): … … 949 958 raise exceptions.ValidationError(msg) 950 959 951 960 def formfield(self, **kwargs): 952 defaults = {'form_class': forms.FloatField} 953 defaults.update(kwargs) 961 if self.choices: 962 defaults = kwargs 963 else: 964 defaults = {'form_class': forms.FloatField} 965 defaults.update(kwargs) 954 966 return super(FloatField, self).formfield(**defaults) 955 967 956 968 class IntegerField(Field): … … 984 996 raise exceptions.ValidationError(msg) 985 997 986 998 def formfield(self, **kwargs): 987 defaults = {'form_class': forms.IntegerField} 988 defaults.update(kwargs) 999 if self.choices: 1000 defaults = kwargs 1001 else: 1002 defaults = {'form_class': forms.IntegerField} 1003 defaults.update(kwargs) 989 1004 return super(IntegerField, self).formfield(**defaults) 990 1005 991 1006 class BigIntegerField(IntegerField): … … 1014 1029 return "IPAddressField" 1015 1030 1016 1031 def formfield(self, **kwargs): 1017 defaults = {'form_class': forms.IPAddressField} 1018 defaults.update(kwargs) 1032 if self.choices: 1033 defaults = kwargs 1034 else: 1035 defaults = {'form_class': forms.IPAddressField} 1036 defaults.update(kwargs) 1019 1037 return super(IPAddressField, self).formfield(**defaults) 1020 1038 1021 1039 class GenericIPAddressField(Field): … … 1054 1072 return value 1055 1073 1056 1074 def formfield(self, **kwargs): 1057 defaults = {'form_class': forms.GenericIPAddressField} 1058 defaults.update(kwargs) 1075 if self.choices: 1076 defaults = kwargs 1077 else: 1078 defaults = {'form_class': forms.GenericIPAddressField} 1079 defaults.update(kwargs) 1059 1080 return super(GenericIPAddressField, self).formfield(**defaults) 1060 1081 1061 1062 1082 class NullBooleanField(Field): 1063 1083 empty_strings_allowed = False 1064 1084 default_error_messages = { … … 1105 1125 1106 1126 def formfield(self, **kwargs): 1107 1127 defaults = { 1108 'form_class': forms.NullBooleanField,1109 1128 'required': not self.blank, 1110 1129 'label': capfirst(self.verbose_name), 1111 1130 'help_text': self.help_text} 1112 1131 defaults.update(kwargs) 1132 if not self.choices: 1133 defaults['form_class'] = forms.NullBooleanField 1113 1134 return super(NullBooleanField, self).formfield(**defaults) 1114 1135 1115 1136 class PositiveIntegerField(IntegerField): … … 1148 1169 return "SlugField" 1149 1170 1150 1171 def formfield(self, **kwargs): 1151 defaults = {'form_class': forms.SlugField} 1172 if self.choices: 1173 defaults = kwargs 1174 else: 1175 defaults = {'form_class': forms.SlugField} 1176 defaults.update(kwargs) 1152 1177 defaults.update(kwargs) 1153 1178 return super(SlugField, self).formfield(**defaults) 1154 1179 … … 1241 1266 return '' if val is None else val.isoformat() 1242 1267 1243 1268 def formfield(self, **kwargs): 1244 defaults = {'form_class': forms.TimeField} 1245 defaults.update(kwargs) 1269 if self.choices: 1270 defaults = kwargs 1271 else: 1272 defaults = {'form_class': forms.TimeField} 1273 defaults.update(kwargs) 1246 1274 return super(TimeField, self).formfield(**defaults) 1247 1275 1248 1276 class URLField(CharField): … … 1258 1286 def formfield(self, **kwargs): 1259 1287 # As with CharField, this will cause URL validation to be performed 1260 1288 # twice. 1261 defaults = { 1262 'form_class': forms.URLField, 1263 } 1264 defaults.update(kwargs) 1289 if self.choices: 1290 defaults = kwargs 1291 else: 1292 defaults = {'form_class': forms.URLField} 1293 defaults.update(kwargs) 1265 1294 return super(URLField, self).formfield(**defaults)