Skip to content

Commit c94b354

Browse files
committed
Merge pull request #3509 from jpadilla/textfield-max-length
Map TextField max_length to CharField
2 parents 1b4a41c + a1dad50 commit c94b354

File tree

2 files changed

+5
-3
lines changed

2 files changed

+5
-3
lines changed

rest_framework/utils/field_mapping.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,8 @@ def get_field_kwargs(field_name, model_field):
123123
# Ensure that max_length is passed explicitly as a keyword arg,
124124
# rather than as a validator.
125125
max_length = getattr(model_field, 'max_length', None)
126-
if max_length is not None and isinstance(model_field, models.CharField):
126+
if max_length is not None and (isinstance(model_field, models.CharField) or
127+
isinstance(model_field, models.TextField)):
127128
kwargs['max_length'] = max_length
128129
validator_kwarg = [
129130
validator for validator in validator_kwarg

tests/test_model_serializer.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ class RegularFieldsModel(models.Model):
6363
positive_small_integer_field = models.PositiveSmallIntegerField()
6464
slug_field = models.SlugField(max_length=100)
6565
small_integer_field = models.SmallIntegerField()
66-
text_field = models.TextField()
66+
text_field = models.TextField(max_length=100)
6767
time_field = models.TimeField()
6868
url_field = models.URLField(max_length=100)
6969
custom_field = CustomField()
@@ -161,11 +161,12 @@ class Meta:
161161
positive_small_integer_field = IntegerField()
162162
slug_field = SlugField(max_length=100)
163163
small_integer_field = IntegerField()
164-
text_field = CharField(style={'base_template': 'textarea.html'})
164+
text_field = CharField(max_length=100, style={'base_template': 'textarea.html'})
165165
time_field = TimeField()
166166
url_field = URLField(max_length=100)
167167
custom_field = ModelField(model_field=<tests.test_model_serializer.CustomField: custom_field>)
168168
""")
169+
169170
self.assertEqual(unicode_repr(TestSerializer()), expected)
170171

171172
def test_field_options(self):

0 commit comments

Comments
 (0)