Ticket #11404: formset-has-changed-2.diff

File formset-has-changed-2.diff, 3.9 KB (added by Michel Sabchuk, 13 years ago)

Patch updated to Django 1.3 with tests and docs.

  • docs/topics/forms/formsets.txt

     
    146146forms in the formset. Validation was performed for each of the two forms, and
    147147the expected error message appears for the second item.
    148148
     149We 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
    149162.. _understanding-the-managementform:
    150163
    151164Understanding the ManagementForm
  • django/forms/formsets.py

     
    297297        """
    298298        pass
    299299
     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
    300309    def add_fields(self, form, index):
    301310        """A hook for adding extra fields on to each form instance."""
    302311        if self.can_order:
  • tests/regressiontests/forms/tests/formsets.py

     
    7373        self.assertTrue(formset.is_valid())
    7474        self.assertEqual([form.cleaned_data for form in formset.forms], [{'votes': 100, 'choice': u'Calexico'}])
    7575
    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.
    7778        formset = ChoiceFormSet()
    7879        self.assertFalse(formset.is_valid())
     80        self.assertFalse(formset.has_changed())
    7981
    8082    def test_formset_validation(self):
    8183        # FormSet instances can also have an error attribute if validation failed for
     
    9395        self.assertFalse(formset.is_valid())
    9496        self.assertEqual(formset.errors, [{'votes': [u'This field is required.']}])
    9597
     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
    96124    def test_formset_initial_data(self):
    97125        # We can also prefill a FormSet with existing data by providing an ``initial``
    98126        # argument to the constructor. ``initial`` should be a list of dicts. By default,
Back to Top