Author: SmileyChris
Date: 2011-04-22 14:18:27 -0700 (Fri, 22 Apr 2011)
New Revision: 16090
Modified:
django/trunk/django/forms/fields.py
django/trunk/docs/ref/forms/fields.txt
django/trunk/tests/regressiontests/forms/tests/fields.py
Log:
Fixed #13584 -- Optionally allow empty files with django.forms.FileField.
Thanks for the patch erickr and closedbracket.
Modified: django/trunk/django/forms/fields.py
===================================================================
--- django/trunk/django/forms/fields.py 2011-04-22 21:05:29 UTC (rev 16089)
+++ django/trunk/django/forms/fields.py 2011-04-22 21:18:27 UTC (rev 16090)
@@ -450,6 +450,7 @@
def __init__(self, *args, **kwargs):
self.max_length = kwargs.pop('max_length', None)
+ self.allow_empty_file = kwargs.pop('allow_empty_file', False)
super(FileField, self).__init__(*args, **kwargs)
def to_python(self, data):
@@ -468,7 +469,7 @@
raise ValidationError(self.error_messages['max_length'] %
error_values)
if not file_name:
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'])
return data
Modified: django/trunk/docs/ref/forms/fields.txt
===================================================================
--- django/trunk/docs/ref/forms/fields.txt 2011-04-22 21:05:29 UTC (rev
16089)
+++ django/trunk/docs/ref/forms/fields.txt 2011-04-22 21:18:27 UTC (rev
16090)
@@ -503,10 +503,15 @@
* Empty value: ``None``
* Normalizes to: An ``UploadedFile`` object that wraps the file content
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``,
``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
documentation </topics/http/file-uploads>`.
Modified: django/trunk/tests/regressiontests/forms/tests/fields.py
===================================================================
--- django/trunk/tests/regressiontests/forms/tests/fields.py 2011-04-22
21:05:29 UTC (rev 16089)
+++ django/trunk/tests/regressiontests/forms/tests/fields.py 2011-04-22
21:18:27 UTC (rev 16090)
@@ -506,6 +506,11 @@
self.assertEqual('files/test2.pdf', f.clean(None, 'files/test2.pdf'))
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
##################################################################
def test_urlfield_1(self):
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/django-updates?hl=en.