Fixed #13584 -- Optionally allow empty files with django.forms.FileField. Thanks for the patch erickr and closedbracket.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@16090 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
c77372cad0
commit
534c427b20
|
@ -450,6 +450,7 @@ class FileField(Field):
|
||||||
|
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
self.max_length = kwargs.pop('max_length', None)
|
self.max_length = kwargs.pop('max_length', None)
|
||||||
|
self.allow_empty_file = kwargs.pop('allow_empty_file', False)
|
||||||
super(FileField, self).__init__(*args, **kwargs)
|
super(FileField, self).__init__(*args, **kwargs)
|
||||||
|
|
||||||
def to_python(self, data):
|
def to_python(self, data):
|
||||||
|
@ -468,7 +469,7 @@ class FileField(Field):
|
||||||
raise ValidationError(self.error_messages['max_length'] % error_values)
|
raise ValidationError(self.error_messages['max_length'] % error_values)
|
||||||
if not file_name:
|
if not file_name:
|
||||||
raise ValidationError(self.error_messages['invalid'])
|
raise ValidationError(self.error_messages['invalid'])
|
||||||
if not file_size:
|
if not self.allow_empty_file and not file_size:
|
||||||
raise ValidationError(self.error_messages['empty'])
|
raise ValidationError(self.error_messages['empty'])
|
||||||
|
|
||||||
return data
|
return data
|
||||||
|
|
|
@ -503,10 +503,15 @@ given length.
|
||||||
* Empty value: ``None``
|
* Empty value: ``None``
|
||||||
* Normalizes to: An ``UploadedFile`` object that wraps the file content
|
* Normalizes to: An ``UploadedFile`` object that wraps the file content
|
||||||
and file name into a single object.
|
and file name into a single object.
|
||||||
* Validates that non-empty file data has been bound to the form.
|
* Can validate that non-empty file data has been bound to the form.
|
||||||
* Error message keys: ``required``, ``invalid``, ``missing``, ``empty``,
|
* Error message keys: ``required``, ``invalid``, ``missing``, ``empty``,
|
||||||
``max_length``
|
``max_length``
|
||||||
|
|
||||||
|
Has two optional arguments for validation, ''max_length'' and
|
||||||
|
''allow_empty_file''. If provided, these ensure that the file name is at
|
||||||
|
most the given length, and that validation will succeed even if the file
|
||||||
|
content is empty.
|
||||||
|
|
||||||
To learn more about the ``UploadedFile`` object, see the :doc:`file uploads
|
To learn more about the ``UploadedFile`` object, see the :doc:`file uploads
|
||||||
documentation </topics/http/file-uploads>`.
|
documentation </topics/http/file-uploads>`.
|
||||||
|
|
||||||
|
|
|
@ -506,6 +506,11 @@ class FieldsTests(TestCase):
|
||||||
self.assertEqual('files/test2.pdf', f.clean(None, 'files/test2.pdf'))
|
self.assertEqual('files/test2.pdf', f.clean(None, 'files/test2.pdf'))
|
||||||
self.assertEqual(SimpleUploadedFile, type(f.clean(SimpleUploadedFile('name', 'Some File Content'))))
|
self.assertEqual(SimpleUploadedFile, type(f.clean(SimpleUploadedFile('name', 'Some File Content'))))
|
||||||
|
|
||||||
|
def test_filefield_3(self):
|
||||||
|
f = FileField(allow_empty_file=True)
|
||||||
|
self.assertEqual(SimpleUploadedFile,
|
||||||
|
type(f.clean(SimpleUploadedFile('name', ''))))
|
||||||
|
|
||||||
# URLField ##################################################################
|
# URLField ##################################################################
|
||||||
|
|
||||||
def test_urlfield_1(self):
|
def test_urlfield_1(self):
|
||||||
|
|
Loading…
Reference in New Issue