|
2 | 2 | from __future__ import unicode_literals
|
3 | 3 |
|
4 | 4 | from django.forms import (CharField, DateField, FileField, Form, IntegerField,
|
5 |
| - ValidationError) |
| 5 | + ValidationError, formsets) |
6 | 6 | from django.forms.formsets import BaseFormSet, formset_factory
|
7 | 7 | from django.forms.util import ErrorList
|
8 | 8 | from django.test import TestCase
|
@@ -51,7 +51,7 @@ def test_basic_formset(self):
|
51 | 51 | # for adding data. By default, it displays 1 blank form. It can display more,
|
52 | 52 | # but we'll look at how to do so later.
|
53 | 53 | formset = ChoiceFormSet(auto_id=False, prefix='choices')
|
54 |
| - self.assertHTMLEqual(str(formset), """<input type="hidden" name="choices-TOTAL_FORMS" value="1" /><input type="hidden" name="choices-INITIAL_FORMS" value="0" /><input type="hidden" name="choices-MAX_NUM_FORMS" /> |
| 54 | + self.assertHTMLEqual(str(formset), """<input type="hidden" name="choices-TOTAL_FORMS" value="1" /><input type="hidden" name="choices-INITIAL_FORMS" value="0" /><input type="hidden" name="choices-MAX_NUM_FORMS" value="1000" /> |
55 | 55 | <tr><th>Choice:</th><td><input type="text" name="choices-0-choice" /></td></tr>
|
56 | 56 | <tr><th>Votes:</th><td><input type="text" name="choices-0-votes" /></td></tr>""")
|
57 | 57 |
|
@@ -654,8 +654,8 @@ def test_limiting_max_forms(self):
|
654 | 654 | # Limiting the maximum number of forms ########################################
|
655 | 655 | # Base case for max_num.
|
656 | 656 |
|
657 |
| - # When not passed, max_num will take its default value of None, i.e. unlimited |
658 |
| - # number of forms, only controlled by the value of the extra parameter. |
| 657 | + # When not passed, max_num will take a high default value, leaving the |
| 658 | + # number of forms only controlled by the value of the extra parameter. |
659 | 659 |
|
660 | 660 | LimitedFavoriteDrinkFormSet = formset_factory(FavoriteDrinkForm, extra=3)
|
661 | 661 | formset = LimitedFavoriteDrinkFormSet()
|
@@ -702,8 +702,8 @@ def test_limiting_max_forms(self):
|
702 | 702 | def test_max_num_with_initial_data(self):
|
703 | 703 | # max_num with initial data
|
704 | 704 |
|
705 |
| - # When not passed, max_num will take its default value of None, i.e. unlimited |
706 |
| - # number of forms, only controlled by the values of the initial and extra |
| 705 | + # When not passed, max_num will take a high default value, leaving the |
| 706 | + # number of forms only controlled by the value of the initial and extra |
707 | 707 | # parameters.
|
708 | 708 |
|
709 | 709 | initial = [
|
@@ -878,6 +878,64 @@ def is_valid(self):
|
878 | 878 | self.assertTrue(formset.is_valid())
|
879 | 879 | self.assertTrue(all([form.is_valid_called for form in formset.forms]))
|
880 | 880 |
|
| 881 | + def test_hard_limit_on_instantiated_forms(self): |
| 882 | + """A formset has a hard limit on the number of forms instantiated.""" |
| 883 | + # reduce the default limit of 1000 temporarily for testing |
| 884 | + _old_DEFAULT_MAX_NUM = formsets.DEFAULT_MAX_NUM |
| 885 | + try: |
| 886 | + formsets.DEFAULT_MAX_NUM = 3 |
| 887 | + ChoiceFormSet = formset_factory(Choice) |
| 888 | + # someone fiddles with the mgmt form data... |
| 889 | + formset = ChoiceFormSet( |
| 890 | + { |
| 891 | + 'choices-TOTAL_FORMS': '4', |
| 892 | + 'choices-INITIAL_FORMS': '0', |
| 893 | + 'choices-MAX_NUM_FORMS': '4', |
| 894 | + 'choices-0-choice': 'Zero', |
| 895 | + 'choices-0-votes': '0', |
| 896 | + 'choices-1-choice': 'One', |
| 897 | + 'choices-1-votes': '1', |
| 898 | + 'choices-2-choice': 'Two', |
| 899 | + 'choices-2-votes': '2', |
| 900 | + 'choices-3-choice': 'Three', |
| 901 | + 'choices-3-votes': '3', |
| 902 | + }, |
| 903 | + prefix='choices', |
| 904 | + ) |
| 905 | + # But we still only instantiate 3 forms |
| 906 | + self.assertEqual(len(formset.forms), 3) |
| 907 | + finally: |
| 908 | + formsets.DEFAULT_MAX_NUM = _old_DEFAULT_MAX_NUM |
| 909 | + |
| 910 | + def test_increase_hard_limit(self): |
| 911 | + """Can increase the built-in forms limit via a higher max_num.""" |
| 912 | + # reduce the default limit of 1000 temporarily for testing |
| 913 | + _old_DEFAULT_MAX_NUM = formsets.DEFAULT_MAX_NUM |
| 914 | + try: |
| 915 | + formsets.DEFAULT_MAX_NUM = 3 |
| 916 | + # for this form, we want a limit of 4 |
| 917 | + ChoiceFormSet = formset_factory(Choice, max_num=4) |
| 918 | + formset = ChoiceFormSet( |
| 919 | + { |
| 920 | + 'choices-TOTAL_FORMS': '4', |
| 921 | + 'choices-INITIAL_FORMS': '0', |
| 922 | + 'choices-MAX_NUM_FORMS': '4', |
| 923 | + 'choices-0-choice': 'Zero', |
| 924 | + 'choices-0-votes': '0', |
| 925 | + 'choices-1-choice': 'One', |
| 926 | + 'choices-1-votes': '1', |
| 927 | + 'choices-2-choice': 'Two', |
| 928 | + 'choices-2-votes': '2', |
| 929 | + 'choices-3-choice': 'Three', |
| 930 | + 'choices-3-votes': '3', |
| 931 | + }, |
| 932 | + prefix='choices', |
| 933 | + ) |
| 934 | + # This time four forms are instantiated |
| 935 | + self.assertEqual(len(formset.forms), 4) |
| 936 | + finally: |
| 937 | + formsets.DEFAULT_MAX_NUM = _old_DEFAULT_MAX_NUM |
| 938 | + |
881 | 939 |
|
882 | 940 | data = {
|
883 | 941 | 'choices-TOTAL_FORMS': '1', # the number of forms rendered
|
|
0 commit comments