Commits

Luke Plant committed 4ece809

[1.2.X] Fixed #15349 - Bound FormSet produces bound empty_form

Thanks to hidde-jan for the report and patch.

Backport of [15614] from trunk.

Comments (0)

Files changed (2)

django/forms/formsets.py

             'prefix': self.add_prefix('__prefix__'),
             'empty_permitted': True,
         }
-        if self.data or self.files:
-            defaults['data'] = self.data
-            defaults['files'] = self.files
         defaults.update(kwargs)
         form = self.form(**defaults)
         self.add_fields(form, None)

tests/regressiontests/forms/tests/formsets.py

 # -*- coding: utf-8 -*-
 from unittest import TestCase
-from django.forms import Form, CharField, IntegerField, ValidationError
+from django.forms import Form, CharField, IntegerField, ValidationError, DateField
 from django.forms.formsets import formset_factory, BaseFormSet
 
 
     formset=BaseFavoriteDrinksFormSet, extra=3)
 
 
+class ArticleForm(Form):
+    title = CharField()
+    pub_date = DateField()
+
+ArticleFormSet = formset_factory(ArticleForm)
+
+
 class FormsFormsetTestCase(TestCase):
     def test_basic_formset(self):
         # A FormSet constructor takes the same arguments as Form. Let's create a FormSet
         self.assertFalse(formset.is_valid())
         self.assertEqual(formset.non_form_errors(), [u'You may only specify a drink once.'])
 
+    def test_empty_forms_are_unbound(self):
+        data = {
+            'form-TOTAL_FORMS': u'1',
+            'form-INITIAL_FORMS': u'0',
+            'form-0-title': u'Test',
+            'form-0-pub_date': u'1904-06-16',
+        }
+        unbound_formset = ArticleFormSet()
+        bound_formset = ArticleFormSet(data)
+
+        empty_forms = []
+
+        empty_forms.append(unbound_formset.empty_form)
+        empty_forms.append(bound_formset.empty_form)
+
+        # Empty forms should be unbound
+        self.assertFalse(empty_forms[0].is_bound)
+        self.assertFalse(empty_forms[1].is_bound)
+
+        # The empty forms should be equal.
+        self.assertEqual(empty_forms[0].as_p(), empty_forms[1].as_p())
+
+    def test_empty_forms_are_unbound(self):
+        data = {
+            'form-TOTAL_FORMS': u'1',
+            'form-INITIAL_FORMS': u'0',
+            'form-0-title': u'Test',
+            'form-0-pub_date': u'1904-06-16',
+        }
+        unbound_formset = ArticleFormSet()
+        bound_formset = ArticleFormSet(data)
+
+        empty_forms = []
+
+        empty_forms.append(unbound_formset.empty_form)
+        empty_forms.append(bound_formset.empty_form)
+
+        # Empty forms should be unbound
+        self.assertFalse(empty_forms[0].is_bound)
+        self.assertFalse(empty_forms[1].is_bound)
+
+        # The empty forms should be equal.
+        self.assertEqual(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):