diff --git a/django/forms/widgets.py b/django/forms/widgets.py index 784ccda159..0a5059a9c2 100644 --- a/django/forms/widgets.py +++ b/django/forms/widgets.py @@ -840,6 +840,11 @@ class MultiWidget(Widget): obj.widgets = copy.deepcopy(self.widgets) return obj + @property + def needs_multipart_form(self): + return any(w.needs_multipart_form for w in self.widgets) + + class SplitDateTimeWidget(MultiWidget): """ A Widget that splits datetime input into two boxes. diff --git a/tests/forms_tests/tests/test_widgets.py b/tests/forms_tests/tests/test_widgets.py index 4c566dc8e4..06ee2d6c53 100644 --- a/tests/forms_tests/tests/test_widgets.py +++ b/tests/forms_tests/tests/test_widgets.py @@ -849,6 +849,14 @@ beatle J R Ringo False""") w = MyMultiWidget(widgets=(TextInput(attrs={'class': 'big'}), TextInput(attrs={'class': 'small'})), attrs={'id': 'bar'}) self.assertHTMLEqual(w.render('name', ['john', 'lennon']), '
') + # Test needs_multipart_form=True if any widget needs it + w = MyMultiWidget(widgets=(TextInput(), FileInput())) + self.assertTrue(w.needs_multipart_form) + + # Test needs_multipart_form=False if no widget needs it + w = MyMultiWidget(widgets=(TextInput(), TextInput())) + self.assertFalse(w.needs_multipart_form) + def test_splitdatetime(self): w = SplitDateTimeWidget() self.assertHTMLEqual(w.render('date', ''), '')