Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Proposed fix for #18162.

  • Loading branch information...
commit b6f4a92ff45d98a63dc29402d8ad86b88e6a6697 1 parent f3ae67a
@oinopion oinopion authored
View
7 django/db/models/fields/__init__.py
@@ -467,7 +467,7 @@ def _get_flatchoices(self):
def save_form_data(self, instance, data):
setattr(instance, self.name, data)
- def formfield(self, form_class=forms.CharField, **kwargs):
+ def formfield(self, form_class=None, **kwargs):
"""
Returns a django.forms.Field instance for this database Field.
"""
@@ -488,7 +488,8 @@ def formfield(self, form_class=forms.CharField, **kwargs):
defaults['coerce'] = self.to_python
if self.null:
defaults['empty_value'] = None
- form_class = forms.TypedChoiceField
+ if form_class is None or not issubclass(form_class, forms.TypedChoiceField):
+ form_class = forms.TypedChoiceField
# Many of the subclass-specific formfield arguments (min_value,
# max_value) don't apply for choice fields, so be sure to only pass
# the values that TypedChoiceField will understand.
@@ -498,6 +499,8 @@ def formfield(self, form_class=forms.CharField, **kwargs):
'error_messages', 'show_hidden_initial'):
del kwargs[k]
defaults.update(kwargs)
+ if form_class is None:
+ form_class = forms.CharField
return form_class(**defaults)
def value_from_object(self, obj):
View
10 tests/regressiontests/model_fields/tests.py
@@ -73,6 +73,16 @@ def test_field_verbose_name(self):
self.assertEqual(m._meta.get_field('id').verbose_name, 'verbose pk')
+ def test_formclass_with_choices(self):
+ # regression for 18162
+ class CustomChoiceField(forms.TypedChoiceField):
+ pass
+ choices = [('a@b.cc', 'a@b.cc'), ('b@b.cc', 'b@b.cc')]
+ field = models.CharField(choices=choices)
+ klass = CustomChoiceField
+ self.assertIsInstance(field.formfield(form_class=klass), klass)
+
+
class DecimalFieldTests(test.TestCase):
def test_to_python(self):
f = models.DecimalField(max_digits=4, decimal_places=2)
Please sign in to comment.
Something went wrong with that request. Please try again.