summaryrefslogtreecommitdiff
path: root/django
diff options
context:
space:
mode:
authorRussell Keith-Magee <russell@keith-magee.com>2010-12-19 13:41:43 +0000
committerRussell Keith-Magee <russell@keith-magee.com>2010-12-19 13:41:43 +0000
commit7adffaeaf6dfa22db0b6b2a29632b9150c7ac732 (patch)
tree760220d871d4246985cdc71ed2879ac82333c4f4 /django
parent059d9205d40243117730aba4baea1373834e57d2 (diff)
Fixed #14655 -- Made formsets iterable. This allows a slightly more natural iteration API (`for form in formsets`), and allows you to easily override the form rendering order. Thanks to Kent Hauser for the suggestion and patch.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@14986 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'django')
-rw-r--r--django/forms/formsets.py17
1 files changed, 14 insertions, 3 deletions
diff --git a/django/forms/formsets.py b/django/forms/formsets.py
index 6d92236686..9a1b5a8cac 100644
--- a/django/forms/formsets.py
+++ b/django/forms/formsets.py
@@ -49,6 +49,17 @@ class BaseFormSet(StrAndUnicode):
def __unicode__(self):
return self.as_table()
+ def __iter__(self):
+ """Yields the forms in the order they should be rendered"""
+ return iter(self.forms)
+
+ def __getitem__(self, index):
+ """Returns the form at the given index, based on the rendering order"""
+ return list(self)[index]
+
+ def __len__(self):
+ return len(self.forms)
+
def _management_form(self):
"""Returns the ManagementForm instance for this FormSet."""
if self.is_bound:
@@ -323,17 +334,17 @@ class BaseFormSet(StrAndUnicode):
# XXX: there is no semantic division between forms here, there
# probably should be. It might make sense to render each form as a
# table row with each field as a td.
- forms = u' '.join([form.as_table() for form in self.forms])
+ forms = u' '.join([form.as_table() for form in self])
return mark_safe(u'\n'.join([unicode(self.management_form), forms]))
def as_p(self):
"Returns this formset rendered as HTML <p>s."
- forms = u' '.join([form.as_p() for form in self.forms])
+ forms = u' '.join([form.as_p() for form in self])
return mark_safe(u'\n'.join([unicode(self.management_form), forms]))
def as_ul(self):
"Returns this formset rendered as HTML <li>s."
- forms = u' '.join([form.as_ul() for form in self.forms])
+ forms = u' '.join([form.as_ul() for form in self])
return mark_safe(u'\n'.join([unicode(self.management_form), forms]))
def formset_factory(form, formset=BaseFormSet, extra=1, can_order=False,