Ticket #14655: formset-iterable-2.diff

File formset-iterable-2.diff, 1.7 KB (added by kenth, 5 years ago)

Improved patch delegating iter to self.forms

  • django/forms/formsets.py

    diff --git a/django/forms/formsets.py b/django/forms/formsets.py
    index 6d92236..6a814d4 100644
    a b class BaseFormSet(StrAndUnicode): 
    4848
    4949    def __unicode__(self):
    5050        return self.as_table()
     51   
     52    def __iter__(self):
     53        """Yields the forms in the order they should be rendered"""
     54        return iter(self.forms)
     55    def __getitem__(self, index):
     56        return list(self)[index]
     57    def __len__(self):
     58        return len(list(self))
    5159
    5260    def _management_form(self):
    5361        """Returns the ManagementForm instance for this FormSet."""
    class BaseFormSet(StrAndUnicode): 
    323331        # XXX: there is no semantic division between forms here, there
    324332        # probably should be. It might make sense to render each form as a
    325333        # table row with each field as a td.
    326         forms = u' '.join([form.as_table() for form in self.forms])
     334        forms = u' '.join([form.as_table() for form in self])
    327335        return mark_safe(u'\n'.join([unicode(self.management_form), forms]))
    328336
    329337    def as_p(self):
    330338        "Returns this formset rendered as HTML <p>s."
    331         forms = u' '.join([form.as_p() for form in self.forms])
     339        forms = u' '.join([form.as_p() for form in self])
    332340        return mark_safe(u'\n'.join([unicode(self.management_form), forms]))
    333341
    334342    def as_ul(self):
    335343        "Returns this formset rendered as HTML <li>s."
    336         forms = u' '.join([form.as_ul() for form in self.forms])
     344        forms = u' '.join([form.as_ul() for form in self])
    337345        return mark_safe(u'\n'.join([unicode(self.management_form), forms]))
    338346
    339347def formset_factory(form, formset=BaseFormSet, extra=1, can_order=False,
Back to Top