[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. git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.2.X@15615 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
c326ec48d8
commit
e63db6597c
@ -133,9 +133,6 @@ class BaseFormSet(StrAndUnicode):
|
||||
'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)
|
||||
|
@ -1,6 +1,6 @@
|
||||
# -*- 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
|
||||
|
||||
|
||||
@ -41,6 +41,13 @@ FavoriteDrinksFormSet = formset_factory(FavoriteDrinkForm,
|
||||
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
|
||||
@ -767,6 +774,50 @@ class FormsFormsetTestCase(TestCase):
|
||||
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):
|
||||
|
Loading…
x
Reference in New Issue
Block a user