Ticket #11418: 11418.patch
File 11418.patch, 5.8 KB (added by , 14 years ago) |
---|
-
django/forms/formsets.py
diff --git a/django/forms/formsets.py b/django/forms/formsets.py
a b 37 37 self.is_bound = data is not None or files is not None 38 38 self.prefix = prefix or self.get_default_prefix() 39 39 self.auto_id = auto_id 40 self.data = data 41 self.files = files 40 self.data = data or {} 41 self.files = files or {} 42 42 self.initial = initial 43 43 self.error_class = error_class 44 44 self._errors = None … … 51 51 52 52 def _management_form(self): 53 53 """Returns the ManagementForm instance for this FormSet.""" 54 if self. data or self.files:54 if self.is_bound: 55 55 form = ManagementForm(self.data, auto_id=self.auto_id, prefix=self.prefix) 56 56 if not form.is_valid(): 57 57 raise ValidationError('ManagementForm data is missing or has been tampered with') … … 66 66 67 67 def total_form_count(self): 68 68 """Returns the total number of forms in this FormSet.""" 69 if self. data or self.files:69 if self.is_bound: 70 70 return self.management_form.cleaned_data[TOTAL_FORM_COUNT] 71 71 else: 72 72 initial_forms = self.initial_form_count() … … 81 81 82 82 def initial_form_count(self): 83 83 """Returns the number of forms that are required in this FormSet.""" 84 if self. data or self.files:84 if self.is_bound: 85 85 return self.management_form.cleaned_data[INITIAL_FORM_COUNT] 86 86 else: 87 87 # Use the length of the inital data if it's there, 0 otherwise. … … 101 101 Instantiates and returns the i-th form instance in a formset. 102 102 """ 103 103 defaults = {'auto_id': self.auto_id, 'prefix': self.add_prefix(i)} 104 if self. data or self.files:104 if self.is_bound: 105 105 defaults['data'] = self.data 106 106 defaults['files'] = self.files 107 107 if self.initial: … … 133 133 'prefix': self.add_prefix('__prefix__'), 134 134 'empty_permitted': True, 135 135 } 136 if self. data or self.files:136 if self.is_bound: 137 137 defaults['data'] = self.data 138 138 defaults['files'] = self.files 139 139 defaults.update(kwargs) -
docs/topics/forms/formsets.txt
diff --git a/docs/topics/forms/formsets.txt b/docs/topics/forms/formsets.txt
a b 100 100 all forms in the formset:: 101 101 102 102 >>> ArticleFormSet = formset_factory(ArticleForm) 103 >>> formset = ArticleFormSet({}) 103 >>> data = { 104 ... 'form-TOTAL_FORMS': u'1', 105 ... 'form-INITIAL_FORMS': u'0', 106 ... 'form-MAX_NUM_FORMS': u'', 107 ... } 108 >>> formset = ArticleFormSet(data) 104 109 >>> formset.is_valid() 105 110 True 106 111 … … 113 118 ... 'form-INITIAL_FORMS': u'0', 114 119 ... 'form-MAX_NUM_FORMS': u'', 115 120 ... 'form-0-title': u'Test', 116 ... 'form-0-pub_date': u'1 6 June 1904',121 ... 'form-0-pub_date': u'1904-06-16', 117 122 ... 'form-1-title': u'Test', 118 123 ... 'form-1-pub_date': u'', # <-- this date is missing but required 119 124 ... } … … 208 213 ... 'form-INITIAL_FORMS': u'0', 209 214 ... 'form-MAX_NUM_FORMS': u'', 210 215 ... 'form-0-title': u'Test', 211 ... 'form-0-pub_date': u'1 6 June 1904',216 ... 'form-0-pub_date': u'1904-06-16', 212 217 ... 'form-1-title': u'Test', 213 ... 'form-1-pub_date': u' 23 June 1912',218 ... 'form-1-pub_date': u'1912-06-23', 214 219 ... } 215 220 >>> formset = ArticleFormSet(data) 216 221 >>> formset.is_valid() -
tests/regressiontests/forms/formsets.py
diff --git a/tests/regressiontests/forms/formsets.py b/tests/regressiontests/forms/formsets.py
a b 5 5 FormSet allows us to use multiple instance of the same form on 1 page. For now, 6 6 the best way to create a FormSet is by using the formset_factory function. 7 7 8 >>> from django.forms import Form, CharField, IntegerField, ValidationError 8 >>> from django.forms import Form, CharField, IntegerField, ValidationError, DateField 9 9 >>> from django.forms.formsets import formset_factory, BaseFormSet 10 10 11 11 >>> class Choice(Form): … … 697 697 >>> formset.management_form.prefix 698 698 'form' 699 699 700 >>> formset = FavoriteDrinksFormSet(data={}) 700 >>> data = { 701 ... 'form-TOTAL_FORMS': '2', # the number of forms rendered 702 ... 'form-INITIAL_FORMS': '0', # the number of forms with initial data 703 ... 'form-MAX_NUM_FORMS': '0', # max number of forms 704 ... } 705 >>> formset = FavoriteDrinksFormSet(data=data) 701 706 >>> formset.management_form.prefix 702 707 'form' 703 708 … … 721 726 >>> print formset.non_form_errors() 722 727 <ul class="errorlist"><li>You may only specify a drink once.</li></ul> 723 728 729 # Regression test for #11418 ################################################# 730 731 >>> class ArticleForm(Form): 732 ... title = CharField() 733 ... pub_date = DateField() 734 735 >>> ArticleFormSet = formset_factory(ArticleForm) 736 >>> formset = ArticleFormSet({}) 737 Traceback (most recent call last): 738 ... 739 ValidationError: [u'ManagementForm data is missing or has been tampered with'] 740 >>> data = { 741 ... 'form-TOTAL_FORMS': u'1', 742 ... 'form-INITIAL_FORMS': u'0', 743 ... } 744 >>> formset = ArticleFormSet(data) 745 >>> formset.initial_form_count() 746 0 747 >>> formset.total_form_count() 748 1 749 >>> formset.is_bound 750 True 751 >>> formset.forms[0].is_bound 752 True 753 >>> formset.is_valid() 754 True 755 >>> formset.forms[0].is_valid() 756 True 757 >>> formset.cleaned_data 758 [{}] 759 >>> data = { 760 ... 'form-TOTAL_FORMS': u'2', 761 ... 'form-INITIAL_FORMS': u'0', 762 ... 'form-0-title': u'Test', 763 ... 'form-0-pub_date': u'1904-06-16', 764 ... 'form-1-title': u'Test', 765 ... 'form-1-pub_date': u'', # <-- this date is missing but required 766 ... } 767 >>> formset = ArticleFormSet(data) 768 >>> formset.is_valid() 769 False 770 >>> formset.errors 771 [{}, {'pub_date': [u'This field is required.']}] 772 724 773 """