|
1 | 1 | # -*- coding: utf-8 -*-
|
2 |
| -from django.forms import Form, CharField, IntegerField, ValidationError, DateField |
| 2 | +from django.forms import Form, CharField, IntegerField, ValidationError, DateField, formsets |
3 | 3 | from django.forms.formsets import formset_factory, BaseFormSet
|
4 | 4 | from django.test import TestCase
|
5 | 5 |
|
@@ -47,7 +47,7 @@ def test_basic_formset(self):
|
47 | 47 | # for adding data. By default, it displays 1 blank form. It can display more,
|
48 | 48 | # but we'll look at how to do so later.
|
49 | 49 | formset = ChoiceFormSet(auto_id=False, prefix='choices')
|
50 |
| - 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" /> |
| 50 | + 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" /> |
51 | 51 | <tr><th>Choice:</th><td><input type="text" name="choices-0-choice" /></td></tr>
|
52 | 52 | <tr><th>Votes:</th><td><input type="text" name="choices-0-votes" /></td></tr>""")
|
53 | 53 |
|
@@ -650,8 +650,8 @@ def test_limiting_max_forms(self):
|
650 | 650 | # Limiting the maximum number of forms ########################################
|
651 | 651 | # Base case for max_num.
|
652 | 652 |
|
653 |
| - # When not passed, max_num will take its default value of None, i.e. unlimited |
654 |
| - # number of forms, only controlled by the value of the extra parameter. |
| 653 | + # When not passed, max_num will take a high default value, leaving the |
| 654 | + # number of forms only controlled by the value of the extra parameter. |
655 | 655 |
|
656 | 656 | LimitedFavoriteDrinkFormSet = formset_factory(FavoriteDrinkForm, extra=3)
|
657 | 657 | formset = LimitedFavoriteDrinkFormSet()
|
@@ -698,8 +698,8 @@ def test_limiting_max_forms(self):
|
698 | 698 | def test_max_num_with_initial_data(self):
|
699 | 699 | # max_num with initial data
|
700 | 700 |
|
701 |
| - # When not passed, max_num will take its default value of None, i.e. unlimited |
702 |
| - # number of forms, only controlled by the values of the initial and extra |
| 701 | + # When not passed, max_num will take a high default value, leaving the |
| 702 | + # number of forms only controlled by the value of the initial and extra |
703 | 703 | # parameters.
|
704 | 704 |
|
705 | 705 | initial = [
|
@@ -844,6 +844,64 @@ def test_formset_nonzero(self):
|
844 | 844 | self.assertEqual(len(formset.forms), 0)
|
845 | 845 | self.assertTrue(formset)
|
846 | 846 |
|
| 847 | + def test_hard_limit_on_instantiated_forms(self): |
| 848 | + """A formset has a hard limit on the number of forms instantiated.""" |
| 849 | + # reduce the default limit of 1000 temporarily for testing |
| 850 | + _old_DEFAULT_MAX_NUM = formsets.DEFAULT_MAX_NUM |
| 851 | + try: |
| 852 | + formsets.DEFAULT_MAX_NUM = 3 |
| 853 | + ChoiceFormSet = formset_factory(Choice) |
| 854 | + # someone fiddles with the mgmt form data... |
| 855 | + formset = ChoiceFormSet( |
| 856 | + { |
| 857 | + 'choices-TOTAL_FORMS': '4', |
| 858 | + 'choices-INITIAL_FORMS': '0', |
| 859 | + 'choices-MAX_NUM_FORMS': '4', |
| 860 | + 'choices-0-choice': 'Zero', |
| 861 | + 'choices-0-votes': '0', |
| 862 | + 'choices-1-choice': 'One', |
| 863 | + 'choices-1-votes': '1', |
| 864 | + 'choices-2-choice': 'Two', |
| 865 | + 'choices-2-votes': '2', |
| 866 | + 'choices-3-choice': 'Three', |
| 867 | + 'choices-3-votes': '3', |
| 868 | + }, |
| 869 | + prefix='choices', |
| 870 | + ) |
| 871 | + # But we still only instantiate 3 forms |
| 872 | + self.assertEqual(len(formset.forms), 3) |
| 873 | + finally: |
| 874 | + formsets.DEFAULT_MAX_NUM = _old_DEFAULT_MAX_NUM |
| 875 | + |
| 876 | + def test_increase_hard_limit(self): |
| 877 | + """Can increase the built-in forms limit via a higher max_num.""" |
| 878 | + # reduce the default limit of 1000 temporarily for testing |
| 879 | + _old_DEFAULT_MAX_NUM = formsets.DEFAULT_MAX_NUM |
| 880 | + try: |
| 881 | + formsets.DEFAULT_MAX_NUM = 3 |
| 882 | + # for this form, we want a limit of 4 |
| 883 | + ChoiceFormSet = formset_factory(Choice, max_num=4) |
| 884 | + formset = ChoiceFormSet( |
| 885 | + { |
| 886 | + 'choices-TOTAL_FORMS': '4', |
| 887 | + 'choices-INITIAL_FORMS': '0', |
| 888 | + 'choices-MAX_NUM_FORMS': '4', |
| 889 | + 'choices-0-choice': 'Zero', |
| 890 | + 'choices-0-votes': '0', |
| 891 | + 'choices-1-choice': 'One', |
| 892 | + 'choices-1-votes': '1', |
| 893 | + 'choices-2-choice': 'Two', |
| 894 | + 'choices-2-votes': '2', |
| 895 | + 'choices-3-choice': 'Three', |
| 896 | + 'choices-3-votes': '3', |
| 897 | + }, |
| 898 | + prefix='choices', |
| 899 | + ) |
| 900 | + # This time four forms are instantiated |
| 901 | + self.assertEqual(len(formset.forms), 4) |
| 902 | + finally: |
| 903 | + formsets.DEFAULT_MAX_NUM = _old_DEFAULT_MAX_NUM |
| 904 | + |
847 | 905 |
|
848 | 906 | data = {
|
849 | 907 | 'choices-TOTAL_FORMS': '1', # the number of forms rendered
|
|
0 commit comments