Ticket #11404: formset-has-changed-2.diff
File formset-has-changed-2.diff, 3.9 KB (added by , 13 years ago) |
---|
-
docs/topics/forms/formsets.txt
146 146 forms in the formset. Validation was performed for each of the two forms, and 147 147 the expected error message appears for the second item. 148 148 149 We can also check if form data differs from the initial data (i.e. the form was sent without any data):: 150 151 >>> data = { 152 ... 'form-TOTAL_FORMS': u'1', 153 ... 'form-INITIAL_FORMS': u'0', 154 ... 'form-MAX_NUM_FORMS': u'', 155 ... 'form-0-title': u'', 156 ... 'form-0-pub_date': u'', 157 ... } 158 >>> formset = ArticleFormSet(data) 159 >>> formset.has_changed() 160 False 161 149 162 .. _understanding-the-managementform: 150 163 151 164 Understanding the ManagementForm -
django/forms/formsets.py
297 297 """ 298 298 pass 299 299 300 def has_changed(self): 301 """ 302 Returns true if data in any form differs from initial. 303 """ 304 for form in self.forms: 305 if form.has_changed(): 306 return True 307 return False 308 300 309 def add_fields(self, form, index): 301 310 """A hook for adding extra fields on to each form instance.""" 302 311 if self.can_order: -
tests/regressiontests/forms/tests/formsets.py
73 73 self.assertTrue(formset.is_valid()) 74 74 self.assertEqual([form.cleaned_data for form in formset.forms], [{'votes': 100, 'choice': u'Calexico'}]) 75 75 76 # If a FormSet was not passed any data, its is_valid method should return False. 76 # If a FormSet was not passed any data, its is_valid and 77 # has_changed methods should return False. 77 78 formset = ChoiceFormSet() 78 79 self.assertFalse(formset.is_valid()) 80 self.assertFalse(formset.has_changed()) 79 81 80 82 def test_formset_validation(self): 81 83 # FormSet instances can also have an error attribute if validation failed for … … 93 95 self.assertFalse(formset.is_valid()) 94 96 self.assertEqual(formset.errors, [{'votes': [u'This field is required.']}]) 95 97 98 def test_formset_has_changed(self): 99 # FormSet instances has_changed method will be True if any data is 100 # passed to his forms, even if the formset didn't validate 101 data = { 102 'choices-TOTAL_FORMS': '1', # the number of forms rendered 103 'choices-INITIAL_FORMS': '0', # the number of forms with initial data 104 'choices-MAX_NUM_FORMS': '0', # max number of forms 105 'choices-0-choice': '', 106 'choices-0-votes': '', 107 } 108 blank_formset = ChoiceFormSet(data, auto_id=False, prefix='choices') 109 self.assertFalse(blank_formset.has_changed()) 110 111 # invalid formset test 112 data.update({'choices-0-choice': 'Calexico'}) 113 invalid_formset = ChoiceFormSet(data, auto_id=False, prefix='choices') 114 self.assertFalse(invalid_formset.is_valid()) 115 self.assertTrue(invalid_formset.has_changed()) 116 117 # valid formset test 118 data.update({'choices-0-votes': '100'}) 119 valid_formset = ChoiceFormSet(data, auto_id=False, prefix='choices') 120 self.assertTrue(valid_formset.is_valid()) 121 self.assertTrue(valid_formset.has_changed()) 122 123 96 124 def test_formset_initial_data(self): 97 125 # We can also prefill a FormSet with existing data by providing an ``initial`` 98 126 # argument to the constructor. ``initial`` should be a list of dicts. By default,