Code

Opened 4 years ago

Closed 3 years ago

Last modified 3 years ago

#13584 closed Bug (fixed)

django.forms FileField does not allow empty files.

Reported by: jamesh Owned by: closedbracket
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 erikr 4 years ago.
allow_empty_file_formfield.diff (2.4 KB) - added by erikr 4 years ago.
13584.diff (2.5 KB) - added by closedbracket 3 years ago.

Download all attachments as: .zip

Change History (14)

comment:1 Changed 4 years ago by jamesh

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

comment:2 Changed 4 years ago by adamnelson

  • Component changed from Uncategorized to File uploads/storage

comment:3 Changed 4 years ago by erikr

  • milestone set to 1.3
  • Needs documentation set
  • Needs tests set
  • Owner changed from nobody to erikr
  • Status changed from new to assigned
  • Triage Stage changed from Unreviewed to Accepted
  • Version changed from 1.1 to SVN

Changed 4 years ago by erikr

comment:4 Changed 4 years ago by erikr

  • Has patch set
  • Needs documentation unset
  • Needs tests unset
  • Owner changed from erikr to nobody
  • Status changed from assigned to new

comment:5 Changed 4 years ago by erikr

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 4 years ago by jamesh

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 3 years ago by closedbracket

  • Owner changed from nobody to closedbracket

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

Changed 3 years ago by closedbracket

comment:8 Changed 3 years ago by jezdez

  • Triage Stage changed from Accepted to Ready for checkin

comment:9 Changed 3 years ago by julien

  • Severity set to Normal
  • Type set to Bug

comment:10 Changed 3 years ago by SmileyChris

  • Resolution set to fixed
  • Status changed from new to closed

In [16090]:

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

comment:11 Changed 3 years ago by jacob

  • milestone 1.3 deleted

Milestone 1.3 deleted

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.