Make ``Formset.__getitem__`` O(1), rather than O(n). If you override ``__iter__`` you now need to also override ``__getitem__`` for consistant behavior. Thanks to Carl and Russ for the review.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@16770 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
5f287f75f2
commit
01b0eb50fd
|
@ -55,7 +55,7 @@ class BaseFormSet(StrAndUnicode):
|
||||||
|
|
||||||
def __getitem__(self, index):
|
def __getitem__(self, index):
|
||||||
"""Returns the form at the given index, based on the rendering order"""
|
"""Returns the form at the given index, based on the rendering order"""
|
||||||
return list(self)[index]
|
return self.forms[index]
|
||||||
|
|
||||||
def __len__(self):
|
def __len__(self):
|
||||||
return len(self.forms)
|
return len(self.forms)
|
||||||
|
|
|
@ -49,6 +49,10 @@ they were created. The default formset iterator also renders the forms
|
||||||
in this order, but you can change this order by providing an alternate
|
in this order, but you can change this order by providing an alternate
|
||||||
implementation for the :meth:`__iter__()` method.
|
implementation for the :meth:`__iter__()` method.
|
||||||
|
|
||||||
|
Formsets can also be indexed into, which returns the corresponding form. If you
|
||||||
|
override ``__iter__``, you will need to also override ``__getitem__`` to have
|
||||||
|
matching behavior.
|
||||||
|
|
||||||
Using initial data with a formset
|
Using initial data with a formset
|
||||||
---------------------------------
|
---------------------------------
|
||||||
|
|
||||||
|
|
|
@ -793,8 +793,10 @@ class FormsFormsetTestCase(TestCase):
|
||||||
# Formets can override the default iteration order
|
# Formets can override the default iteration order
|
||||||
class BaseReverseFormSet(BaseFormSet):
|
class BaseReverseFormSet(BaseFormSet):
|
||||||
def __iter__(self):
|
def __iter__(self):
|
||||||
for form in reversed(self.forms):
|
return reversed(self.forms)
|
||||||
yield form
|
|
||||||
|
def __getitem__(self, idx):
|
||||||
|
return super(BaseReverseFormSet, self).__getitem__(len(self) - idx - 1)
|
||||||
|
|
||||||
ReverseChoiceFormset = formset_factory(Choice, BaseReverseFormSet, extra=3)
|
ReverseChoiceFormset = formset_factory(Choice, BaseReverseFormSet, extra=3)
|
||||||
reverse_formset = ReverseChoiceFormset()
|
reverse_formset = ReverseChoiceFormset()
|
||||||
|
|
Loading…
Reference in New Issue