From 3fc43c964ef7ab52261ec3a164d66b19f06f5cea Mon Sep 17 00:00:00 2001 From: Simon Charette Date: Thu, 3 Jan 2013 15:13:51 +0100 Subject: [PATCH] Fixed #19545 -- Make sure media/is_multipart work with empty formsets --- django/forms/formsets.py | 7 +++++-- tests/regressiontests/forms/tests/formsets.py | 19 ++++++++++++++++--- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/django/forms/formsets.py b/django/forms/formsets.py index 219a80edee..ee9a2b5f63 100644 --- a/django/forms/formsets.py +++ b/django/forms/formsets.py @@ -333,7 +333,10 @@ class BaseFormSet(object): Returns True if the formset needs to be multipart, i.e. it has FileInput. Otherwise, False. """ - return self.forms and self.forms[0].is_multipart() + if self.forms: + return self.forms[0].is_multipart() + else: + return self.empty_form.is_multipart() @property def media(self): @@ -342,7 +345,7 @@ class BaseFormSet(object): if self.forms: return self.forms[0].media else: - return Media() + return self.empty_form.media def as_table(self): "Returns this formset rendered as HTML s -- excluding the
." diff --git a/tests/regressiontests/forms/tests/formsets.py b/tests/regressiontests/forms/tests/formsets.py index bf893c4c1d..ef6f40c3e3 100644 --- a/tests/regressiontests/forms/tests/formsets.py +++ b/tests/regressiontests/forms/tests/formsets.py @@ -1,8 +1,9 @@ # -*- coding: utf-8 -*- from __future__ import unicode_literals -from django.forms import Form, CharField, IntegerField, ValidationError, DateField -from django.forms.formsets import formset_factory, BaseFormSet +from django.forms import (CharField, DateField, FileField, Form, IntegerField, + ValidationError) +from django.forms.formsets import BaseFormSet, formset_factory from django.forms.util import ErrorList from django.test import TestCase @@ -974,11 +975,23 @@ class TestIsBoundBehavior(TestCase): self.assertHTMLEqual(empty_forms[0].as_p(), empty_forms[1].as_p()) class TestEmptyFormSet(TestCase): - "Test that an empty formset still calls clean()" def test_empty_formset_is_valid(self): + """Test that an empty formset still calls clean()""" EmptyFsetWontValidateFormset = formset_factory(FavoriteDrinkForm, extra=0, formset=EmptyFsetWontValidate) formset = EmptyFsetWontValidateFormset(data={'form-INITIAL_FORMS':'0', 'form-TOTAL_FORMS':'0'},prefix="form") formset2 = EmptyFsetWontValidateFormset(data={'form-INITIAL_FORMS':'0', 'form-TOTAL_FORMS':'1', 'form-0-name':'bah' },prefix="form") self.assertFalse(formset.is_valid()) self.assertFalse(formset2.is_valid()) + def test_empty_formset_media(self): + """Make sure media is available on empty formset, refs #19545""" + class MediaForm(Form): + class Media: + js = ('some-file.js',) + self.assertIn('some-file.js', str(formset_factory(MediaForm, extra=0)().media)) + + def test_empty_formset_is_multipart(self): + """Make sure `is_multipart()` works with empty formset, refs #19545""" + class FileForm(Form): + file = FileField() + self.assertTrue(formset_factory(FileForm, extra=0)().is_multipart())