Ticket #14655: formset-iterable-tests.diff

File formset-iterable-tests.diff, 34.6 KB (added by kenth, 5 years ago)
  • tests/modeltests/model_formsets/tests.py

    diff --git a/tests/modeltests/model_formsets/tests.py b/tests/modeltests/model_formsets/tests.py
    index 396f1bd..20bd384 100644
    a b class ModelFormsetTest(TestCase): 
    9292        AuthorFormSet = modelformset_factory(Author, extra=3)
    9393
    9494        formset = AuthorFormSet(queryset=qs)
    95         self.assertEqual(len(formset.forms), 3)
    96         self.assertEqual(formset.forms[0].as_p(),
     95        self.assertEqual(len(formset), 3)
     96        self.assertEqual(formset[0].as_p(),
    9797            '<p><label for="id_form-0-name">Name:</label> <input id="id_form-0-name" type="text" name="form-0-name" maxlength="100" /><input type="hidden" name="form-0-id" id="id_form-0-id" /></p>')
    98         self.assertEqual(formset.forms[1].as_p(),
     98        self.assertEqual(formset[1].as_p(),
    9999            '<p><label for="id_form-1-name">Name:</label> <input id="id_form-1-name" type="text" name="form-1-name" maxlength="100" /><input type="hidden" name="form-1-id" id="id_form-1-id" /></p>')
    100         self.assertEqual(formset.forms[2].as_p(),
     100        self.assertEqual(formset[2].as_p(),
    101101            '<p><label for="id_form-2-name">Name:</label> <input id="id_form-2-name" type="text" name="form-2-name" maxlength="100" /><input type="hidden" name="form-2-id" id="id_form-2-id" /></p>')
    102102
    103103        data = {
    class ModelFormsetTest(TestCase): 
    130130        AuthorFormSet = modelformset_factory(Author, extra=1, can_delete=False)
    131131
    132132        formset = AuthorFormSet(queryset=qs)
    133         self.assertEqual(len(formset.forms), 3)
    134         self.assertEqual(formset.forms[0].as_p(),
     133        self.assertEqual(len(formset), 3)
     134        self.assertEqual(formset[0].as_p(),
    135135            '<p><label for="id_form-0-name">Name:</label> <input id="id_form-0-name" type="text" name="form-0-name" value="Arthur Rimbaud" maxlength="100" /><input type="hidden" name="form-0-id" value="%d" id="id_form-0-id" /></p>' % author2.id)
    136         self.assertEqual(formset.forms[1].as_p(),
     136        self.assertEqual(formset[1].as_p(),
    137137            '<p><label for="id_form-1-name">Name:</label> <input id="id_form-1-name" type="text" name="form-1-name" value="Charles Baudelaire" maxlength="100" /><input type="hidden" name="form-1-id" value="%d" id="id_form-1-id" /></p>' % author1.id)
    138         self.assertEqual(formset.forms[2].as_p(),
     138        self.assertEqual(formset[2].as_p(),
    139139            '<p><label for="id_form-2-name">Name:</label> <input id="id_form-2-name" type="text" name="form-2-name" maxlength="100" /><input type="hidden" name="form-2-id" id="id_form-2-id" /></p>')
    140140
    141141        data = {
    class ModelFormsetTest(TestCase): 
    168168        AuthorFormSet = modelformset_factory(Author, extra=1, can_delete=True)
    169169
    170170        formset = AuthorFormSet(queryset=qs)
    171         self.assertEqual(len(formset.forms), 4)
    172         self.assertEqual(formset.forms[0].as_p(),
     171        self.assertEqual(len(formset), 4)
     172        self.assertEqual(formset[0].as_p(),
    173173            '<p><label for="id_form-0-name">Name:</label> <input id="id_form-0-name" type="text" name="form-0-name" value="Arthur Rimbaud" maxlength="100" /></p>\n'
    174174            '<p><label for="id_form-0-DELETE">Delete:</label> <input type="checkbox" name="form-0-DELETE" id="id_form-0-DELETE" /><input type="hidden" name="form-0-id" value="%d" id="id_form-0-id" /></p>' % author2.id)
    175         self.assertEqual(formset.forms[1].as_p(),
     175        self.assertEqual(formset[1].as_p(),
    176176            '<p><label for="id_form-1-name">Name:</label> <input id="id_form-1-name" type="text" name="form-1-name" value="Charles Baudelaire" maxlength="100" /></p>\n'
    177177            '<p><label for="id_form-1-DELETE">Delete:</label> <input type="checkbox" name="form-1-DELETE" id="id_form-1-DELETE" /><input type="hidden" name="form-1-id" value="%d" id="id_form-1-id" /></p>' % author1.id)
    178         self.assertEqual(formset.forms[2].as_p(),
     178        self.assertEqual(formset[2].as_p(),
    179179            '<p><label for="id_form-2-name">Name:</label> <input id="id_form-2-name" type="text" name="form-2-name" value="Paul Verlaine" maxlength="100" /></p>\n'
    180180            '<p><label for="id_form-2-DELETE">Delete:</label> <input type="checkbox" name="form-2-DELETE" id="id_form-2-DELETE" /><input type="hidden" name="form-2-id" value="%d" id="id_form-2-id" /></p>' % author3.id)
    181         self.assertEqual(formset.forms[3].as_p(),
     181        self.assertEqual(formset[3].as_p(),
    182182            '<p><label for="id_form-3-name">Name:</label> <input id="id_form-3-name" type="text" name="form-3-name" maxlength="100" /></p>\n'
    183183            '<p><label for="id_form-3-DELETE">Delete:</label> <input type="checkbox" name="form-3-DELETE" id="id_form-3-DELETE" /><input type="hidden" name="form-3-id" id="id_form-3-id" /></p>')
    184184
    class ModelFormsetTest(TestCase): 
    285285
    286286        AuthorFormSet = modelformset_factory(Author, max_num=None, extra=3)
    287287        formset = AuthorFormSet(queryset=qs)
    288         self.assertEqual(len(formset.forms), 6)
     288        self.assertEqual(len(formset), 6)
    289289        self.assertEqual(len(formset.extra_forms), 3)
    290290
    291291        AuthorFormSet = modelformset_factory(Author, max_num=4, extra=3)
    292292        formset = AuthorFormSet(queryset=qs)
    293         self.assertEqual(len(formset.forms), 4)
     293        self.assertEqual(len(formset), 4)
    294294        self.assertEqual(len(formset.extra_forms), 1)
    295295
    296296        AuthorFormSet = modelformset_factory(Author, max_num=0, extra=3)
    297297        formset = AuthorFormSet(queryset=qs)
    298         self.assertEqual(len(formset.forms), 3)
     298        self.assertEqual(len(formset), 3)
    299299        self.assertEqual(len(formset.extra_forms), 0)
    300300
    301301        AuthorFormSet = modelformset_factory(Author, max_num=None)
    class ModelFormsetTest(TestCase): 
    356356    def test_model_inheritance(self):
    357357        BetterAuthorFormSet = modelformset_factory(BetterAuthor)
    358358        formset = BetterAuthorFormSet()
    359         self.assertEqual(len(formset.forms), 1)
    360         self.assertEqual(formset.forms[0].as_p(),
     359        self.assertEqual(len(formset), 1)
     360        self.assertEqual(formset[0].as_p(),
    361361            '<p><label for="id_form-0-name">Name:</label> <input id="id_form-0-name" type="text" name="form-0-name" maxlength="100" /></p>\n'
    362362            '<p><label for="id_form-0-write_speed">Write speed:</label> <input type="text" name="form-0-write_speed" id="id_form-0-write_speed" /><input type="hidden" name="form-0-author_ptr" id="id_form-0-author_ptr" /></p>')
    363363
    class ModelFormsetTest(TestCase): 
    379379        hemingway_id = BetterAuthor.objects.get(name="Ernest Hemingway").pk
    380380
    381381        formset = BetterAuthorFormSet()
    382         self.assertEqual(len(formset.forms), 2)
    383         self.assertEqual(formset.forms[0].as_p(),
     382        self.assertEqual(len(formset), 2)
     383        self.assertEqual(formset[0].as_p(),
    384384            '<p><label for="id_form-0-name">Name:</label> <input id="id_form-0-name" type="text" name="form-0-name" value="Ernest Hemingway" maxlength="100" /></p>\n'
    385385            '<p><label for="id_form-0-write_speed">Write speed:</label> <input type="text" name="form-0-write_speed" value="10" id="id_form-0-write_speed" /><input type="hidden" name="form-0-author_ptr" value="%d" id="id_form-0-author_ptr" /></p>' % hemingway_id)
    386         self.assertEqual(formset.forms[1].as_p(),
     386        self.assertEqual(formset[1].as_p(),
    387387            '<p><label for="id_form-1-name">Name:</label> <input id="id_form-1-name" type="text" name="form-1-name" maxlength="100" /></p>\n'
    388388            '<p><label for="id_form-1-write_speed">Write speed:</label> <input type="text" name="form-1-write_speed" id="id_form-1-write_speed" /><input type="hidden" name="form-1-author_ptr" id="id_form-1-author_ptr" /></p>')
    389389
    class ModelFormsetTest(TestCase): 
    411411        author = Author.objects.create(name='Charles Baudelaire')
    412412
    413413        formset = AuthorBooksFormSet(instance=author)
    414         self.assertEqual(len(formset.forms), 3)
    415         self.assertEqual(formset.forms[0].as_p(),
     414        self.assertEqual(len(formset), 3)
     415        self.assertEqual(formset[0].as_p(),
    416416            '<p><label for="id_book_set-0-title">Title:</label> <input id="id_book_set-0-title" type="text" name="book_set-0-title" maxlength="100" /><input type="hidden" name="book_set-0-author" value="%d" id="id_book_set-0-author" /><input type="hidden" name="book_set-0-id" id="id_book_set-0-id" /></p>'  % author.id)
    417         self.assertEqual(formset.forms[1].as_p(),
     417        self.assertEqual(formset[1].as_p(),
    418418            '<p><label for="id_book_set-1-title">Title:</label> <input id="id_book_set-1-title" type="text" name="book_set-1-title" maxlength="100" /><input type="hidden" name="book_set-1-author" value="%d" id="id_book_set-1-author" /><input type="hidden" name="book_set-1-id" id="id_book_set-1-id" /></p>' % author.id)
    419         self.assertEqual(formset.forms[2].as_p(),
     419        self.assertEqual(formset[2].as_p(),
    420420            '<p><label for="id_book_set-2-title">Title:</label> <input id="id_book_set-2-title" type="text" name="book_set-2-title" maxlength="100" /><input type="hidden" name="book_set-2-author" value="%d" id="id_book_set-2-author" /><input type="hidden" name="book_set-2-id" id="id_book_set-2-id" /></p>' % author.id)
    421421
    422422        data = {
    class ModelFormsetTest(TestCase): 
    445445        author = Author.objects.get(name='Charles Baudelaire')
    446446
    447447        formset = AuthorBooksFormSet(instance=author)
    448         self.assertEqual(len(formset.forms), 3)
    449         self.assertEqual(formset.forms[0].as_p(),
     448        self.assertEqual(len(formset), 3)
     449        self.assertEqual(formset[0].as_p(),
    450450            '<p><label for="id_book_set-0-title">Title:</label> <input id="id_book_set-0-title" type="text" name="book_set-0-title" value="Les Fleurs du Mal" maxlength="100" /><input type="hidden" name="book_set-0-author" value="%d" id="id_book_set-0-author" /><input type="hidden" name="book_set-0-id" value="%d" id="id_book_set-0-id" /></p>' % (author.id, book1.id))
    451         self.assertEqual(formset.forms[1].as_p(),
     451        self.assertEqual(formset[1].as_p(),
    452452            '<p><label for="id_book_set-1-title">Title:</label> <input id="id_book_set-1-title" type="text" name="book_set-1-title" maxlength="100" /><input type="hidden" name="book_set-1-author" value="%d" id="id_book_set-1-author" /><input type="hidden" name="book_set-1-id" id="id_book_set-1-id" /></p>' % author.id)
    453         self.assertEqual(formset.forms[2].as_p(),
     453        self.assertEqual(formset[2].as_p(),
    454454            '<p><label for="id_book_set-2-title">Title:</label> <input id="id_book_set-2-title" type="text" name="book_set-2-title" maxlength="100" /><input type="hidden" name="book_set-2-author" value="%d" id="id_book_set-2-author" /><input type="hidden" name="book_set-2-id" id="id_book_set-2-id" /></p>' % author.id)
    455455
    456456        data = {
    class ModelFormsetTest(TestCase): 
    509509        # Test using a custom prefix on an inline formset.
    510510
    511511        formset = AuthorBooksFormSet(prefix="test")
    512         self.assertEqual(len(formset.forms), 2)
    513         self.assertEqual(formset.forms[0].as_p(),
     512        self.assertEqual(len(formset), 2)
     513        self.assertEqual(formset[0].as_p(),
    514514            '<p><label for="id_test-0-title">Title:</label> <input id="id_test-0-title" type="text" name="test-0-title" maxlength="100" /><input type="hidden" name="test-0-author" id="id_test-0-author" /><input type="hidden" name="test-0-id" id="id_test-0-id" /></p>')
    515         self.assertEqual(formset.forms[1].as_p(),
     515        self.assertEqual(formset[1].as_p(),
    516516            '<p><label for="id_test-1-title">Title:</label> <input id="id_test-1-title" type="text" name="test-1-title" maxlength="100" /><input type="hidden" name="test-1-author" id="id_test-1-author" /><input type="hidden" name="test-1-id" id="id_test-1-id" /></p>')
    517517
    518518    def test_inline_formsets_with_custom_pk(self):
    class ModelFormsetTest(TestCase): 
    523523        author = Author.objects.create(pk=1, name='Charles Baudelaire')
    524524
    525525        formset = AuthorBooksFormSet2(instance=author)
    526         self.assertEqual(len(formset.forms), 1)
    527         self.assertEqual(formset.forms[0].as_p(),
     526        self.assertEqual(len(formset), 1)
     527        self.assertEqual(formset[0].as_p(),
    528528            '<p><label for="id_bookwithcustompk_set-0-my_pk">My pk:</label> <input type="text" name="bookwithcustompk_set-0-my_pk" id="id_bookwithcustompk_set-0-my_pk" /></p>\n'
    529529            '<p><label for="id_bookwithcustompk_set-0-title">Title:</label> <input id="id_bookwithcustompk_set-0-title" type="text" name="bookwithcustompk_set-0-title" maxlength="100" /><input type="hidden" name="bookwithcustompk_set-0-author" value="1" id="id_bookwithcustompk_set-0-author" /></p>')
    530530
    class ModelFormsetTest(TestCase): 
    555555        author = Author.objects.create(pk=1, name='Charles Baudelaire')
    556556
    557557        formset = AuthorBooksFormSet3(instance=author)
    558         self.assertEqual(len(formset.forms), 1)
    559         self.assertEqual(formset.forms[0].as_p(),
     558        self.assertEqual(len(formset), 1)
     559        self.assertEqual(formset[0].as_p(),
    560560            '<p><label for="id_alternatebook_set-0-title">Title:</label> <input id="id_alternatebook_set-0-title" type="text" name="alternatebook_set-0-title" maxlength="100" /></p>\n'
    561561            '<p><label for="id_alternatebook_set-0-notes">Notes:</label> <input id="id_alternatebook_set-0-notes" type="text" name="alternatebook_set-0-notes" maxlength="100" /><input type="hidden" name="alternatebook_set-0-author" value="1" id="id_alternatebook_set-0-author" /><input type="hidden" name="alternatebook_set-0-book_ptr" id="id_alternatebook_set-0-book_ptr" /></p>')
    562562
    class ModelFormsetTest(TestCase): 
    643643
    644644        custom_qs = Book.objects.order_by('-title')
    645645        formset = AuthorBooksFormSet(instance=author, queryset=custom_qs)
    646         self.assertEqual(len(formset.forms), 5)
    647         self.assertEqual(formset.forms[0].as_p(),
     646        self.assertEqual(len(formset), 5)
     647        self.assertEqual(formset[0].as_p(),
    648648            '<p><label for="id_book_set-0-title">Title:</label> <input id="id_book_set-0-title" type="text" name="book_set-0-title" value="Les Paradis Artificiels" maxlength="100" /><input type="hidden" name="book_set-0-author" value="1" id="id_book_set-0-author" /><input type="hidden" name="book_set-0-id" value="1" id="id_book_set-0-id" /></p>')
    649         self.assertEqual(formset.forms[1].as_p(),
     649        self.assertEqual(formset[1].as_p(),
    650650            '<p><label for="id_book_set-1-title">Title:</label> <input id="id_book_set-1-title" type="text" name="book_set-1-title" value="Les Fleurs du Mal" maxlength="100" /><input type="hidden" name="book_set-1-author" value="1" id="id_book_set-1-author" /><input type="hidden" name="book_set-1-id" value="2" id="id_book_set-1-id" /></p>')
    651         self.assertEqual(formset.forms[2].as_p(),
     651        self.assertEqual(formset[2].as_p(),
    652652            '<p><label for="id_book_set-2-title">Title:</label> <input id="id_book_set-2-title" type="text" name="book_set-2-title" value="Flowers of Evil" maxlength="100" /><input type="hidden" name="book_set-2-author" value="1" id="id_book_set-2-author" /><input type="hidden" name="book_set-2-id" value="3" id="id_book_set-2-id" /></p>')
    653         self.assertEqual(formset.forms[3].as_p(),
     653        self.assertEqual(formset[3].as_p(),
    654654            '<p><label for="id_book_set-3-title">Title:</label> <input id="id_book_set-3-title" type="text" name="book_set-3-title" maxlength="100" /><input type="hidden" name="book_set-3-author" value="1" id="id_book_set-3-author" /><input type="hidden" name="book_set-3-id" id="id_book_set-3-id" /></p>')
    655         self.assertEqual(formset.forms[4].as_p(),
     655        self.assertEqual(formset[4].as_p(),
    656656            '<p><label for="id_book_set-4-title">Title:</label> <input id="id_book_set-4-title" type="text" name="book_set-4-title" maxlength="100" /><input type="hidden" name="book_set-4-author" value="1" id="id_book_set-4-author" /><input type="hidden" name="book_set-4-id" id="id_book_set-4-id" /></p>')
    657657
    658658        data = {
    class ModelFormsetTest(TestCase): 
    673673
    674674        custom_qs = Book.objects.filter(title__startswith='F')
    675675        formset = AuthorBooksFormSet(instance=author, queryset=custom_qs)
    676         self.assertEqual(formset.forms[0].as_p(),
     676        self.assertEqual(formset[0].as_p(),
    677677            '<p><label for="id_book_set-0-title">Title:</label> <input id="id_book_set-0-title" type="text" name="book_set-0-title" value="Flowers of Evil" maxlength="100" /><input type="hidden" name="book_set-0-author" value="1" id="id_book_set-0-author" /><input type="hidden" name="book_set-0-id" value="3" id="id_book_set-0-id" /></p>')
    678         self.assertEqual(formset.forms[1].as_p(),
     678        self.assertEqual(formset[1].as_p(),
    679679            '<p><label for="id_book_set-1-title">Title:</label> <input id="id_book_set-1-title" type="text" name="book_set-1-title" maxlength="100" /><input type="hidden" name="book_set-1-author" value="1" id="id_book_set-1-author" /><input type="hidden" name="book_set-1-id" id="id_book_set-1-id" /></p>')
    680         self.assertEqual(formset.forms[2].as_p(),
     680        self.assertEqual(formset[2].as_p(),
    681681            '<p><label for="id_book_set-2-title">Title:</label> <input id="id_book_set-2-title" type="text" name="book_set-2-title" maxlength="100" /><input type="hidden" name="book_set-2-author" value="1" id="id_book_set-2-author" /><input type="hidden" name="book_set-2-id" id="id_book_set-2-id" /></p>')
    682682
    683683        data = {
    class ModelFormsetTest(TestCase): 
    697697
    698698        CustomPrimaryKeyFormSet = modelformset_factory(CustomPrimaryKey)
    699699        formset = CustomPrimaryKeyFormSet()
    700         self.assertEqual(len(formset.forms), 1)
    701         self.assertEqual(formset.forms[0].as_p(),
     700        self.assertEqual(len(formset), 1)
     701        self.assertEqual(formset[0].as_p(),
    702702            '<p><label for="id_form-0-my_pk">My pk:</label> <input id="id_form-0-my_pk" type="text" name="form-0-my_pk" maxlength="10" /></p>\n'
    703703            '<p><label for="id_form-0-some_field">Some field:</label> <input id="id_form-0-some_field" type="text" name="form-0-some_field" maxlength="100" /></p>')
    704704
    class ModelFormsetTest(TestCase): 
    708708
    709709        FormSet = inlineformset_factory(Place, Owner, extra=2, can_delete=False)
    710710        formset = FormSet(instance=place)
    711         self.assertEqual(len(formset.forms), 2)
    712         self.assertEqual(formset.forms[0].as_p(),
     711        self.assertEqual(len(formset), 2)
     712        self.assertEqual(formset[0].as_p(),
    713713            '<p><label for="id_owner_set-0-name">Name:</label> <input id="id_owner_set-0-name" type="text" name="owner_set-0-name" maxlength="100" /><input type="hidden" name="owner_set-0-place" value="1" id="id_owner_set-0-place" /><input type="hidden" name="owner_set-0-auto_id" id="id_owner_set-0-auto_id" /></p>')
    714         self.assertEqual(formset.forms[1].as_p(),
     714        self.assertEqual(formset[1].as_p(),
    715715            '<p><label for="id_owner_set-1-name">Name:</label> <input id="id_owner_set-1-name" type="text" name="owner_set-1-name" maxlength="100" /><input type="hidden" name="owner_set-1-place" value="1" id="id_owner_set-1-place" /><input type="hidden" name="owner_set-1-auto_id" id="id_owner_set-1-auto_id" /></p>')
    716716
    717717        data = {
    class ModelFormsetTest(TestCase): 
    732732        self.assertEqual(owner.place.name, 'Giordanos')
    733733
    734734        formset = FormSet(instance=place)
    735         self.assertEqual(len(formset.forms), 3)
    736         self.assertEqual(formset.forms[0].as_p(),
     735        self.assertEqual(len(formset), 3)
     736        self.assertEqual(formset[0].as_p(),
    737737            '<p><label for="id_owner_set-0-name">Name:</label> <input id="id_owner_set-0-name" type="text" name="owner_set-0-name" value="Joe Perry" maxlength="100" /><input type="hidden" name="owner_set-0-place" value="1" id="id_owner_set-0-place" /><input type="hidden" name="owner_set-0-auto_id" value="1" id="id_owner_set-0-auto_id" /></p>')
    738         self.assertEqual(formset.forms[1].as_p(),
     738        self.assertEqual(formset[1].as_p(),
    739739            '<p><label for="id_owner_set-1-name">Name:</label> <input id="id_owner_set-1-name" type="text" name="owner_set-1-name" maxlength="100" /><input type="hidden" name="owner_set-1-place" value="1" id="id_owner_set-1-place" /><input type="hidden" name="owner_set-1-auto_id" id="id_owner_set-1-auto_id" /></p>')
    740         self.assertEqual(formset.forms[2].as_p(),
     740        self.assertEqual(formset[2].as_p(),
    741741            '<p><label for="id_owner_set-2-name">Name:</label> <input id="id_owner_set-2-name" type="text" name="owner_set-2-name" maxlength="100" /><input type="hidden" name="owner_set-2-place" value="1" id="id_owner_set-2-place" /><input type="hidden" name="owner_set-2-auto_id" id="id_owner_set-2-auto_id" /></p>')
    742742
    743743        data = {
    class ModelFormsetTest(TestCase): 
    763763
    764764        FormSet = modelformset_factory(OwnerProfile)
    765765        formset = FormSet()
    766         self.assertEqual(formset.forms[0].as_p(),
     766        self.assertEqual(formset[0].as_p(),
    767767            '<p><label for="id_form-0-owner">Owner:</label> <select name="form-0-owner" id="id_form-0-owner">\n'
    768768            '<option value="" selected="selected">---------</option>\n'
    769769            '<option value="1">Joe Perry at Giordanos</option>\n'
    class ModelFormsetTest(TestCase): 
    776776        self.assertEqual(FormSet.max_num, 1)
    777777
    778778        formset = FormSet(instance=owner)
    779         self.assertEqual(len(formset.forms), 1)
    780         self.assertEqual(formset.forms[0].as_p(),
     779        self.assertEqual(len(formset), 1)
     780        self.assertEqual(formset[0].as_p(),
    781781            '<p><label for="id_ownerprofile-0-age">Age:</label> <input type="text" name="ownerprofile-0-age" id="id_ownerprofile-0-age" /><input type="hidden" name="ownerprofile-0-owner" value="1" id="id_ownerprofile-0-owner" /></p>')
    782782
    783783        data = {
    class ModelFormsetTest(TestCase): 
    796796        self.assertEqual(profile1.age, 54)
    797797
    798798        formset = FormSet(instance=owner)
    799         self.assertEqual(len(formset.forms), 1)
    800         self.assertEqual(formset.forms[0].as_p(),
     799        self.assertEqual(len(formset), 1)
     800        self.assertEqual(formset[0].as_p(),
    801801            '<p><label for="id_ownerprofile-0-age">Age:</label> <input type="text" name="ownerprofile-0-age" value="54" id="id_ownerprofile-0-age" /><input type="hidden" name="ownerprofile-0-owner" value="1" id="id_ownerprofile-0-owner" /></p>')
    802802
    803803        data = {
    class ModelFormsetTest(TestCase): 
    824824        self.assertEqual(FormSet.max_num, 1)
    825825
    826826        formset = FormSet(instance=place)
    827         self.assertEqual(len(formset.forms), 1)
    828         self.assertEqual(formset.forms[0].as_p(),
     827        self.assertEqual(len(formset), 1)
     828        self.assertEqual(formset[0].as_p(),
    829829            '<p><label for="id_location_set-0-lat">Lat:</label> <input id="id_location_set-0-lat" type="text" name="location_set-0-lat" maxlength="100" /></p>\n'
    830830            '<p><label for="id_location_set-0-lon">Lon:</label> <input id="id_location_set-0-lon" type="text" name="location_set-0-lon" maxlength="100" /><input type="hidden" name="location_set-0-place" value="1" id="id_location_set-0-place" /><input type="hidden" name="location_set-0-id" id="id_location_set-0-id" /></p>')
    831831
    class ModelFormsetTest(TestCase): 
    946946        # default. This is required to ensure the value is tested for change correctly
    947947        # when determine what extra forms have changed to save.
    948948
    949         self.assertEquals(len(formset.forms), 1) # this formset only has one form
    950         form = formset.forms[0]
     949        self.assertEquals(len(formset), 1) # this formset only has one form
     950        form = formset[0]
    951951        now = form.fields['date_joined'].initial()
    952952        result = form.as_p()
    953953        result = re.sub(r'\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}(?:\.\d+)?', '__DATETIME__', result)
  • tests/regressiontests/forms/tests/formsets.py

    diff --git a/tests/regressiontests/forms/tests/formsets.py b/tests/regressiontests/forms/tests/formsets.py
    index bf7124f..c806ec8 100644
    a b class FormsFormsetTestCase(TestCase): 
    6666
    6767        formset = ChoiceFormSet(data, auto_id=False, prefix='choices')
    6868        self.assertTrue(formset.is_valid())
    69         self.assertEqual([form.cleaned_data for form in formset.forms], [{'votes': 100, 'choice': u'Calexico'}])
     69        self.assertEqual([form.cleaned_data for form in formset], [{'votes': 100, 'choice': u'Calexico'}])
    7070
    7171        # If a FormSet was not passed any data, its is_valid method should return False.
    7272        formset = ChoiceFormSet()
    class FormsFormsetTestCase(TestCase): 
    9797        formset = ChoiceFormSet(initial=initial, auto_id=False, prefix='choices')
    9898        form_output = []
    9999
    100         for form in formset.forms:
     100        for form in formset:
    101101            form_output.append(form.as_ul())
    102102
    103103        self.assertEqual('\n'.join(form_output), """<li>Choice: <input type="text" name="choices-0-choice" value="Calexico" /></li>
    class FormsFormsetTestCase(TestCase): 
    119119
    120120        formset = ChoiceFormSet(data, auto_id=False, prefix='choices')
    121121        self.assertTrue(formset.is_valid())
    122         self.assertEqual([form.cleaned_data for form in formset.forms], [{'votes': 100, 'choice': u'Calexico'}, {}])
     122        self.assertEqual([form.cleaned_data for form in formset], [{'votes': 100, 'choice': u'Calexico'}, {}])
    123123
    124124    def test_second_form_partially_filled(self):
    125125        # But the second form was blank! Shouldn't we get some errors? No. If we display
    class FormsFormsetTestCase(TestCase): 
    170170        formset = ChoiceFormSet(auto_id=False, prefix='choices')
    171171        form_output = []
    172172
    173         for form in formset.forms:
     173        for form in formset:
    174174            form_output.append(form.as_ul())
    175175
    176176        self.assertEqual('\n'.join(form_output), """<li>Choice: <input type="text" name="choices-0-choice" /></li>
    class FormsFormsetTestCase(TestCase): 
    198198
    199199        formset = ChoiceFormSet(data, auto_id=False, prefix='choices')
    200200        self.assertTrue(formset.is_valid())
    201         self.assertEqual([form.cleaned_data for form in formset.forms], [{}, {}, {}])
     201        self.assertEqual([form.cleaned_data for form in formset], [{}, {}, {}])
    202202
    203203    def test_single_form_completed(self):
    204204        # We can just fill out one of the forms.
    class FormsFormsetTestCase(TestCase): 
    218218        ChoiceFormSet = formset_factory(Choice, extra=3)
    219219        formset = ChoiceFormSet(data, auto_id=False, prefix='choices')
    220220        self.assertTrue(formset.is_valid())
    221         self.assertEqual([form.cleaned_data for form in formset.forms], [{'votes': 100, 'choice': u'Calexico'}, {}, {}])
     221        self.assertEqual([form.cleaned_data for form in formset], [{'votes': 100, 'choice': u'Calexico'}, {}, {}])
    222222
    223223    def test_second_form_partially_filled_2(self):
    224224        # And once again, if we try to partially complete a form, validation will fail.
    class FormsFormsetTestCase(TestCase): 
    261261        formset = ChoiceFormSet(initial=initial, auto_id=False, prefix='choices')
    262262        form_output = []
    263263
    264         for form in formset.forms:
     264        for form in formset:
    265265            form_output.append(form.as_ul())
    266266
    267267        self.assertEqual('\n'.join(form_output), """<li>Choice: <input type="text" name="choices-0-choice" value="Calexico" /></li>
    class FormsFormsetTestCase(TestCase): 
    291291        formset = ChoiceFormSet(initial=initial, auto_id=False, prefix='choices')
    292292        form_output = []
    293293
    294         for form in formset.forms:
     294        for form in formset:
    295295            form_output.append(form.as_ul())
    296296
    297297        self.assertEqual('\n'.join(form_output), """<li>Choice: <input type="text" name="choices-0-choice" value="Calexico" /></li>
    class FormsFormsetTestCase(TestCase): 
    324324
    325325        formset = ChoiceFormSet(data, auto_id=False, prefix='choices')
    326326        self.assertTrue(formset.is_valid())
    327         self.assertEqual([form.cleaned_data for form in formset.forms], [{'votes': 100, 'DELETE': False, 'choice': u'Calexico'}, {'votes': 900, 'DELETE': True, 'choice': u'Fergie'}, {}])
     327        self.assertEqual([form.cleaned_data for form in formset], [{'votes': 100, 'DELETE': False, 'choice': u'Calexico'}, {'votes': 900, 'DELETE': True, 'choice': u'Fergie'}, {}])
    328328        self.assertEqual([form.cleaned_data for form in formset.deleted_forms], [{'votes': 900, 'DELETE': True, 'choice': u'Fergie'}])
    329329
    330330        # If we fill a form with something and then we check the can_delete checkbox for
    class FormsFormsetTestCase(TestCase): 
    376376        # FormSets with ordering ######################################################
    377377        # We can also add ordering ability to a FormSet with an argument to
    378378        # formset_factory. This will add a integer field to each form instance. When
    379         # form validation succeeds, [form.cleaned_data for form in formset.forms] will have the data in the correct
     379        # form validation succeeds, [form.cleaned_data for form in formset] will have the data in the correct
    380380        # order specified by the ordering fields. If a number is duplicated in the set
    381381        # of ordering fields, for instance form 0 and form 3 are both marked as 1, then
    382382        # the form index used as a secondary ordering criteria. In order to put
    class FormsFormsetTestCase(TestCase): 
    388388        formset = ChoiceFormSet(initial=initial, auto_id=False, prefix='choices')
    389389        form_output = []
    390390
    391         for form in formset.forms:
     391        for form in formset:
    392392            form_output.append(form.as_ul())
    393393
    394394        self.assertEqual('\n'.join(form_output), """<li>Choice: <input type="text" name="choices-0-choice" value="Calexico" /></li>
    class FormsFormsetTestCase(TestCase): 
    499499        formset = ChoiceFormSet(initial=initial, auto_id=False, prefix='choices')
    500500        form_output = []
    501501
    502         for form in formset.forms:
     502        for form in formset:
    503503            form_output.append(form.as_ul())
    504504
    505505        self.assertEqual('\n'.join(form_output), """<li>Choice: <input type="text" name="choices-0-choice" value="Calexico" /></li>
    class FormsFormsetTestCase(TestCase): 
    625625        formset = LimitedFavoriteDrinkFormSet()
    626626        form_output = []
    627627
    628         for form in formset.forms:
     628        for form in formset:
    629629            form_output.append(str(form))
    630630
    631631        self.assertEqual('\n'.join(form_output), """<tr><th><label for="id_form-0-name">Name:</label></th><td><input type="text" name="form-0-name" id="id_form-0-name" /></td></tr>
    class FormsFormsetTestCase(TestCase): 
    637637        formset = LimitedFavoriteDrinkFormSet()
    638638        form_output = []
    639639
    640         for form in formset.forms:
     640        for form in formset:
    641641            form_output.append(str(form))
    642642
    643643        self.assertEqual('\n'.join(form_output), "")
    class FormsFormsetTestCase(TestCase): 
    646646        formset = LimitedFavoriteDrinkFormSet()
    647647        form_output = []
    648648
    649         for form in formset.forms:
     649        for form in formset:
    650650            form_output.append(str(form))
    651651
    652652        self.assertEqual('\n'.join(form_output), """<tr><th><label for="id_form-0-name">Name:</label></th><td><input type="text" name="form-0-name" id="id_form-0-name" /></td></tr>
    class FormsFormsetTestCase(TestCase): 
    658658        formset = LimitedFavoriteDrinkFormSet()
    659659        form_output = []
    660660
    661         for form in formset.forms:
     661        for form in formset:
    662662            form_output.append(str(form))
    663663
    664664        self.assertEqual('\n'.join(form_output), """<tr><th><label for="id_form-0-name">Name:</label></th><td><input type="text" name="form-0-name" id="id_form-0-name" /></td></tr>""")
    class FormsFormsetTestCase(TestCase): 
    677677        formset = LimitedFavoriteDrinkFormSet(initial=initial)
    678678        form_output = []
    679679
    680         for form in formset.forms:
     680        for form in formset:
    681681            form_output.append(str(form))
    682682
    683683        self.assertEqual('\n'.join(form_output), """<tr><th><label for="id_form-0-name">Name:</label></th><td><input type="text" name="form-0-name" value="Fernet and Coke" id="id_form-0-name" /></td></tr>
    class FormsFormsetTestCase(TestCase): 
    695695        formset = LimitedFavoriteDrinkFormSet(initial=initial)
    696696        form_output = []
    697697
    698         for form in formset.forms:
     698        for form in formset:
    699699            form_output.append(str(form))
    700700
    701701        self.assertEqual('\n'.join(form_output), "")
    class FormsFormsetTestCase(TestCase): 
    713713        formset = LimitedFavoriteDrinkFormSet(initial=initial)
    714714        form_output = []
    715715
    716         for form in formset.forms:
     716        for form in formset:
    717717            form_output.append(str(form))
    718718
    719719        self.assertEqual('\n'.join(form_output), """<tr><th><label for="id_form-0-name">Name:</label></th><td><input type="text" name="form-0-name" value="Gin Tonic" id="id_form-0-name" /></td></tr>
    class FormsFormsetTestCase(TestCase): 
    728728        formset = LimitedFavoriteDrinkFormSet(initial=initial)
    729729        form_output = []
    730730
    731         for form in formset.forms:
     731        for form in formset:
    732732            form_output.append(str(form))
    733733
    734734        self.assertEqual('\n'.join(form_output), """<tr><th><label for="id_form-0-name">Name:</label></th><td><input type="text" name="form-0-name" value="Gin Tonic" id="id_form-0-name" /></td></tr>
    class FormsFormsetTestCase(TestCase): 
    762762        self.assertFalse(formset.is_valid())
    763763        self.assertEqual(formset.non_form_errors(), [u'You may only specify a drink once.'])
    764764
     765    def test_iteration(self):
     766        # Regression tests for #16455 -- formset instance iterable
     767        extra = 3
     768        ChoiceFormset = formset_factory(Choice, extra=extra)       
     769        formset = ChoiceFormset()
     770       
     771        # confirm iterated formset yields formset.forms
     772        forms = list(formset)
     773        self.assertEqual(forms, formset.forms)
     774        self.assertEqual(len(formset), len(forms))
     775
     776        # confirm indexing of formset
     777        self.assertEqual(formset[0], forms[0])
     778        with self.assertRaises(IndexError):
     779            formset[extra]
     780
     781        # define formset which renders forms in reverse order
     782        class BaseReverseFormSet(BaseFormSet):
     783            def __iter__(self):
     784                for form in reversed(self.forms):
     785                    yield form
     786       
     787        ReverseChoiceFormset = formset_factory(Choice, BaseReverseFormSet, extra=extra)
     788        reverse_formset = ReverseChoiceFormset()
     789       
     790        # confirm that __iter__ modifies rendering order
     791        # compare forms from "reverse" formset with forms from original formset
     792        self.assertEqual(str(reverse_formset[0]), str(forms[-1]))
     793        self.assertEqual(str(reverse_formset[1]), str(forms[-2]))
     794        self.assertEqual(len(reverse_formset), len(forms))
     795       
    765796
    766797data = {
    767798    'choices-TOTAL_FORMS': '1', # the number of forms rendered
Back to Top