Opened 6 years ago

Closed 5 years ago

Last modified 5 years ago

#13584 closed Bug (fixed)

django.forms FileField does not allow empty files.

Reported by: James Henstridge Owned by: Flaviu Simihaian
Component: File uploads/storage Version: master
Severity: Normal Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

The FileField.clean() method raises ValidationError if the uploaded file is empty.

While this is useful in many cases, there are times when it is useful to allow empty files. Since the check is mixed in with the other validation checks, it isn't very easy to disable the check in a subclass without reimplementing the entire clean() method.

It would be nice if FileField could be directly configured to disable the empty check.

Attachments (3)

allow_empty_file_formfield_sloppy_test.diff (2.4 KB) - added by Erik Romijn 6 years ago.
allow_empty_file_formfield.diff (2.4 KB) - added by Erik Romijn 6 years ago.
13584.diff (2.5 KB) - added by Flaviu Simihaian 6 years ago.

Download all attachments as: .zip

Change History (14)

comment:1 Changed 6 years ago by James Henstridge

Needs documentation: unset
Needs tests: unset
Patch needs improvement: unset

comment:2 Changed 6 years ago by Adam Nelson

Component: UncategorizedFile uploads/storage

comment:3 Changed 6 years ago by Erik Romijn

milestone: 1.3
Needs documentation: set
Needs tests: set
Owner: changed from nobody to Erik Romijn
Status: newassigned
Triage Stage: UnreviewedAccepted
Version: 1.1SVN

Changed 6 years ago by Erik Romijn

Changed 6 years ago by Erik Romijn

comment:4 Changed 6 years ago by Erik Romijn

Has patch: set
Needs documentation: unset
Needs tests: unset
Owner: changed from Erik Romijn to nobody
Status: assignednew

comment:5 Changed 6 years ago by Erik Romijn

I've been asked offline why I called the parameter allow_empty_file.

The consideration for this is to have a clear distinction between "must you upload a file, for the form validation to succeed" and "must the file be greater than 0 bytes". By calling it "blank", as suggested, for me this would feel like the former, where allow_empty file clearly refers to the latter.

comment:6 Changed 6 years ago by James Henstridge

The attached patch looks like it would suit my needs. The documentation change looks slightly wrong: it says that FileField has a single optional argument (allow_empty_file). But looking at the code, there is a second optional argument (max_length).

comment:7 Changed 6 years ago by Flaviu Simihaian

Owner: changed from nobody to Flaviu Simihaian

I updated the patch, test, and docs for 1.3.

Changed 6 years ago by Flaviu Simihaian

Attachment: 13584.diff added

comment:8 Changed 6 years ago by Jannis Leidel

Triage Stage: AcceptedReady for checkin

comment:9 Changed 5 years ago by Julien Phalip

Severity: Normal
Type: Bug

comment:10 Changed 5 years ago by Chris Beaven

Resolution: fixed
Status: newclosed

In [16090]:

Fixed #13584 -- Optionally allow empty files with django.forms.FileField. Thanks for the patch erickr and closedbracket.

comment:11 Changed 5 years ago by Jacob

milestone: 1.3

Milestone 1.3 deleted

Note: See TracTickets for help on using tickets.
Back to Top