From 4353640ea9495d58fabd0357253b82de3b069408 Mon Sep 17 00:00:00 2001 From: Tim Graham Date: Fri, 3 Feb 2017 11:11:39 -0500 Subject: [PATCH] Fixed #27805 -- Fixed ClearableFileInput's "Clear" checkbox on model fields with a default. --- django/forms/widgets.py | 6 ++++++ docs/releases/1.10.6.txt | 3 ++- tests/forms_tests/widget_tests/test_clearablefileinput.py | 6 ++++++ 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/django/forms/widgets.py b/django/forms/widgets.py index c32653a245e..38ed56950ac 100644 --- a/django/forms/widgets.py +++ b/django/forms/widgets.py @@ -420,6 +420,12 @@ class ClearableFileInput(FileInput): def use_required_attribute(self, initial): return super().use_required_attribute(initial) and not initial + def value_omitted_from_data(self, data, files, name): + return ( + super().value_omitted_from_data(data, files, name) and + self.clear_checkbox_name(name) not in data + ) + class Textarea(Widget): template_name = 'django/forms/widgets/textarea.html' diff --git a/docs/releases/1.10.6.txt b/docs/releases/1.10.6.txt index 5cd502b3f6f..dc5c49938fc 100644 --- a/docs/releases/1.10.6.txt +++ b/docs/releases/1.10.6.txt @@ -9,4 +9,5 @@ Django 1.10.6 fixes several bugs in 1.10.5. Bugfixes ======== -* ... +* Fixed ``ClearableFileInput``’s "Clear" checkbox on model form fields where + the model field has a ``default`` (:ticket:`27805`). diff --git a/tests/forms_tests/widget_tests/test_clearablefileinput.py b/tests/forms_tests/widget_tests/test_clearablefileinput.py index 3250f6e98e9..09307e95563 100644 --- a/tests/forms_tests/widget_tests/test_clearablefileinput.py +++ b/tests/forms_tests/widget_tests/test_clearablefileinput.py @@ -143,3 +143,9 @@ class ClearableFileInputTest(WidgetTest): # user to keep the existing, initial value. self.assertIs(self.widget.use_required_attribute(None), True) self.assertIs(self.widget.use_required_attribute('resume.txt'), False) + + def test_value_omitted_from_data(self): + widget = ClearableFileInput() + self.assertIs(widget.value_omitted_from_data({}, {}, 'field'), True) + self.assertIs(widget.value_omitted_from_data({}, {'field': 'x'}, 'field'), False) + self.assertIs(widget.value_omitted_from_data({'field-clear': 'y'}, {}, 'field'), False)