Opened 7 years ago

Closed 6 years ago

#12106 closed (fixed)

FileFields ignore initial data when required=False

Reported by: Dave Hall Owned by: nobody
Component: Forms Version: 1.1
Severity: Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: yes Patch needs improvement: no
Easy pickings: UI/UX:


If you create a form containing a FileField where required=False, then the field will ignore any initial value passed in.

from django import forms
from django.core.files import File

class MyForm(forms.Form):
    file_field = forms.FileField(required=False)

test_file = File(open("test.txt"))

form = MyForm(data={}, initial={"file_field": test_file})

form.is_valid()  # Force form validation.

print form.cleaned_data["file_field"]  # prints None

Given that an initial value was supplied, the cleaned_data should now contain the initial value. However, it contains None, as the initial value has been silently discarded.

Attachments (1)

patch.txt (540 bytes) - added by Dave Hall 7 years ago.

Download all attachments as: .zip

Change History (4)

Changed 7 years ago by Dave Hall

Attachment: patch.txt added

comment:1 Changed 7 years ago by Russell Keith-Magee

Needs documentation: unset
Needs tests: set
Patch needs improvement: unset
Triage Stage: UnreviewedAccepted

comment:2 Changed 6 years ago by Dave Hall

Looking over the Django source code, it would appear that this issue has been fixed. Many thanks!

Would it be okay to mark this as fixed?

comment:3 Changed 6 years ago by Russell Keith-Magee

Resolution: fixed
Status: newclosed

If you believe an issue has been resolved, then mark it closed. Trac is completely open for all to modify for exactly this reason. If it turns out you are mistaken, the original reporter can always reopen. In this case, you *are* the original reporter, so that's not likely to happen :-)

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