diff --git a/django/db/models/fields/files.py b/django/db/models/fields/files.py
index cfc4347..4385336 100644
a
|
b
|
class FileField(Field):
|
222 | 222 | setattr(instance, self.name, data) |
223 | 223 | |
224 | 224 | def formfield(self, **kwargs): |
225 | | defaults = {'form_class': forms.FileField} |
| 225 | defaults = {'form_class': forms.FileField, 'max_length': self.max_length} |
226 | 226 | # If a file has been provided previously, then the form doesn't require |
227 | 227 | # that a new file is provided this time. |
228 | 228 | # The code to mark the form field as not required is used by |
diff --git a/django/forms/fields.py b/django/forms/fields.py
index a414e76..dc71c57 100644
a
|
b
|
class FileField(Field):
|
447 | 447 | 'invalid': _(u"No file was submitted. Check the encoding type on the form."), |
448 | 448 | 'missing': _(u"No file was submitted."), |
449 | 449 | 'empty': _(u"The submitted file is empty."), |
| 450 | 'max_length': _(u'Ensure this value has at most %(max)d characters (it has %(length)d).'), |
450 | 451 | } |
451 | 452 | |
452 | 453 | def __init__(self, *args, **kwargs): |
| 454 | self.max_length = kwargs.pop('max_length', None) |
453 | 455 | super(FileField, self).__init__(*args, **kwargs) |
454 | 456 | |
455 | 457 | def clean(self, data, initial=None): |
… |
… |
class FileField(Field):
|
466 | 468 | except AttributeError: |
467 | 469 | raise ValidationError(self.error_messages['invalid']) |
468 | 470 | |
| 471 | if self.max_length is not None and len(file_name) > self.max_length: |
| 472 | error_values = {'max': self.max_length, 'length': len(file_name)} |
| 473 | raise ValidationError(self.error_messages['max_length'] % error_values) |
469 | 474 | if not file_name: |
470 | 475 | raise ValidationError(self.error_messages['invalid']) |
471 | 476 | if not file_size: |
diff --git a/tests/modeltests/model_forms/models.py b/tests/modeltests/model_forms/models.py
index 4075250..c540319 100644
a
|
b
|
class PhoneNumber(models.Model):
|
93 | 93 | |
94 | 94 | class TextFile(models.Model): |
95 | 95 | description = models.CharField(max_length=20) |
96 | | file = models.FileField(storage=temp_storage, upload_to='tests') |
| 96 | file = models.FileField(storage=temp_storage, upload_to='tests', max_length=15) |
97 | 97 | |
98 | 98 | def __unicode__(self): |
99 | 99 | return self.description |
… |
… |
True
|
1018 | 1018 | >>> instance.file |
1019 | 1019 | <FieldFile: tests/test1.txt> |
1020 | 1020 | |
| 1021 | # Check if the max_length attribute has been inherited from the model. |
| 1022 | >>> f = TextFileForm(data={'description': u'Assistance'}, files={'file': SimpleUploadedFile('test-maxlength.txt', 'hello world')}) |
| 1023 | >>> f.is_valid() |
| 1024 | False |
| 1025 | |
1021 | 1026 | # Edit an instance that already has the file defined in the model. This will not |
1022 | 1027 | # save the file again, but leave it exactly as it is. |
1023 | 1028 | |
diff --git a/tests/regressiontests/forms/fields.py b/tests/regressiontests/forms/fields.py
index fa5a62a..ac6ab5a 100644
a
|
b
|
ValidationError: [u'The submitted file is empty.']
|
845 | 845 | >>> type(f.clean(SimpleUploadedFile('name', 'Some File Content'), 'files/test4.pdf')) |
846 | 846 | <class 'django.core.files.uploadedfile.SimpleUploadedFile'> |
847 | 847 | |
| 848 | >>> f = FileField(max_length = 5) |
| 849 | >>> f.clean(SimpleUploadedFile('test_maxlength.txt', 'hello world')) |
| 850 | Traceback (most recent call last): |
| 851 | ... |
| 852 | ValidationError: [u'Ensure this value has at most 5 characters (it has 18).'] |
| 853 | |
| 854 | >>> f.clean('', 'files/test1.pdf') |
| 855 | 'files/test1.pdf' |
| 856 | |
| 857 | >>> f.clean(None, 'files/test2.pdf') |
| 858 | 'files/test2.pdf' |
| 859 | |
| 860 | >>> type(f.clean(SimpleUploadedFile('name', 'Some File Content'))) |
| 861 | <class 'django.core.files.uploadedfile.SimpleUploadedFile'> |
| 862 | |
848 | 863 | # URLField ################################################################## |
849 | 864 | |
850 | 865 | >>> f = URLField() |