Author: Alex
Date: 2011-09-09 19:42:05 -0700 (Fri, 09 Sep 2011)
New Revision: 16773
Modified:
django/trunk/django/forms/formsets.py
django/trunk/docs/topics/forms/formsets.txt
django/trunk/tests/regressiontests/forms/tests/formsets.py
Log:
Fixed #11404. Added ``FormSet.has_changed``, for consistancy with
``Form.has_changed``. Thanks to michelts for the patch.
Modified: django/trunk/django/forms/formsets.py
===================================================================
--- django/trunk/django/forms/formsets.py 2011-09-10 01:55:49 UTC (rev
16772)
+++ django/trunk/django/forms/formsets.py 2011-09-10 02:42:05 UTC (rev
16773)
@@ -300,6 +300,12 @@
"""
pass
+ def has_changed(self):
+ """
+ Returns true if data in any form differs from initial.
+ """
+ return any(form.has_changed() for form in self)
+
def add_fields(self, form, index):
"""A hook for adding extra fields on to each form instance."""
if self.can_order:
Modified: django/trunk/docs/topics/forms/formsets.txt
===================================================================
--- django/trunk/docs/topics/forms/formsets.txt 2011-09-10 01:55:49 UTC (rev
16772)
+++ django/trunk/docs/topics/forms/formsets.txt 2011-09-10 02:42:05 UTC (rev
16773)
@@ -150,6 +150,20 @@
forms in the formset. Validation was performed for each of the two forms, and
the expected error message appears for the second item.
+We can also check if form data differs from the initial data (i.e. the form was
+sent without any data)::
+
+ >>> data = {
+ ... 'form-TOTAL_FORMS': u'1',
+ ... 'form-INITIAL_FORMS': u'0',
+ ... 'form-MAX_NUM_FORMS': u'',
+ ... 'form-0-title': u'',
+ ... 'form-0-pub_date': u'',
+ ... }
+ >>> formset = ArticleFormSet(data)
+ >>> formset.has_changed()
+ False
+
.. _understanding-the-managementform:
Understanding the ManagementForm
Modified: django/trunk/tests/regressiontests/forms/tests/formsets.py
===================================================================
--- django/trunk/tests/regressiontests/forms/tests/formsets.py 2011-09-10
01:55:49 UTC (rev 16772)
+++ django/trunk/tests/regressiontests/forms/tests/formsets.py 2011-09-10
02:42:05 UTC (rev 16773)
@@ -73,9 +73,11 @@
self.assertTrue(formset.is_valid())
self.assertEqual([form.cleaned_data for form in formset.forms],
[{'votes': 100, 'choice': u'Calexico'}])
- # If a FormSet was not passed any data, its is_valid method should
return False.
+ # If a FormSet was not passed any data, its is_valid and has_changed
+ # methods should return False.
formset = ChoiceFormSet()
self.assertFalse(formset.is_valid())
+ self.assertFalse(formset.has_changed())
def test_formset_validation(self):
# FormSet instances can also have an error attribute if validation
failed for
@@ -93,6 +95,31 @@
self.assertFalse(formset.is_valid())
self.assertEqual(formset.errors, [{'votes': [u'This field is
required.']}])
+ def test_formset_has_changed(self):
+ # FormSet instances has_changed method will be True if any data is
+ # passed to his forms, even if the formset didn't validate
+ data = {
+ 'choices-TOTAL_FORMS': '1', # the number of forms rendered
+ 'choices-INITIAL_FORMS': '0', # the number of forms with initial
data
+ 'choices-MAX_NUM_FORMS': '0', # max number of forms
+ 'choices-0-choice': '',
+ 'choices-0-votes': '',
+ }
+ blank_formset = ChoiceFormSet(data, auto_id=False, prefix='choices')
+ self.assertFalse(blank_formset.has_changed())
+
+ # invalid formset test
+ data['choices-0-choice'] = 'Calexico'
+ invalid_formset = ChoiceFormSet(data, auto_id=False, prefix='choices')
+ self.assertFalse(invalid_formset.is_valid())
+ self.assertTrue(invalid_formset.has_changed())
+
+ # valid formset test
+ data['choices-0-votes'] = '100'
+ valid_formset = ChoiceFormSet(data, auto_id=False, prefix='choices')
+ self.assertTrue(valid_formset.is_valid())
+ self.assertTrue(valid_formset.has_changed())
+
def test_formset_initial_data(self):
# We can also prefill a FormSet with existing data by providing an
``initial``
# argument to the constructor. ``initial`` should be a list of dicts.
By default,
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/django-updates?hl=en.