|
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.utils.unittest 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.assertEqual(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.assertEqual(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 |
|
@@ -623,8 +623,8 @@ def test_limiting_max_forms(self):
|
623 | 623 | # Limiting the maximum number of forms ########################################
|
624 | 624 | # Base case for max_num.
|
625 | 625 |
|
626 |
| - # When not passed, max_num will take its default value of None, i.e. unlimited |
627 |
| - # number of forms, only controlled by the value of the extra parameter. |
| 626 | + # When not passed, max_num will take a high default value, leaving the |
| 627 | + # number of forms only controlled by the value of the extra parameter. |
628 | 628 |
|
629 | 629 | LimitedFavoriteDrinkFormSet = formset_factory(FavoriteDrinkForm, extra=3)
|
630 | 630 | formset = LimitedFavoriteDrinkFormSet()
|
@@ -671,8 +671,8 @@ def test_limiting_max_forms(self):
|
671 | 671 | def test_max_num_with_initial_data(self):
|
672 | 672 | # max_num with initial data
|
673 | 673 |
|
674 |
| - # When not passed, max_num will take its default value of None, i.e. unlimited |
675 |
| - # number of forms, only controlled by the values of the initial and extra |
| 674 | + # When not passed, max_num will take a high default value, leaving the |
| 675 | + # number of forms only controlled by the value of the initial and extra |
676 | 676 | # parameters.
|
677 | 677 |
|
678 | 678 | initial = [
|
@@ -805,6 +805,65 @@ def __iter__(self):
|
805 | 805 | self.assertEqual(str(reverse_formset[1]), str(forms[-2]))
|
806 | 806 | self.assertEqual(len(reverse_formset), len(forms))
|
807 | 807 |
|
| 808 | + def test_hard_limit_on_instantiated_forms(self): |
| 809 | + """A formset has a hard limit on the number of forms instantiated.""" |
| 810 | + # reduce the default limit of 1000 temporarily for testing |
| 811 | + _old_DEFAULT_MAX_NUM = formsets.DEFAULT_MAX_NUM |
| 812 | + try: |
| 813 | + formsets.DEFAULT_MAX_NUM = 3 |
| 814 | + ChoiceFormSet = formset_factory(Choice) |
| 815 | + # someone fiddles with the mgmt form data... |
| 816 | + formset = ChoiceFormSet( |
| 817 | + { |
| 818 | + 'choices-TOTAL_FORMS': '4', |
| 819 | + 'choices-INITIAL_FORMS': '0', |
| 820 | + 'choices-MAX_NUM_FORMS': '4', |
| 821 | + 'choices-0-choice': 'Zero', |
| 822 | + 'choices-0-votes': '0', |
| 823 | + 'choices-1-choice': 'One', |
| 824 | + 'choices-1-votes': '1', |
| 825 | + 'choices-2-choice': 'Two', |
| 826 | + 'choices-2-votes': '2', |
| 827 | + 'choices-3-choice': 'Three', |
| 828 | + 'choices-3-votes': '3', |
| 829 | + }, |
| 830 | + prefix='choices', |
| 831 | + ) |
| 832 | + # But we still only instantiate 3 forms |
| 833 | + self.assertEqual(len(formset.forms), 3) |
| 834 | + finally: |
| 835 | + formsets.DEFAULT_MAX_NUM = _old_DEFAULT_MAX_NUM |
| 836 | + |
| 837 | + def test_increase_hard_limit(self): |
| 838 | + """Can increase the built-in forms limit via a higher max_num.""" |
| 839 | + # reduce the default limit of 1000 temporarily for testing |
| 840 | + _old_DEFAULT_MAX_NUM = formsets.DEFAULT_MAX_NUM |
| 841 | + try: |
| 842 | + formsets.DEFAULT_MAX_NUM = 3 |
| 843 | + # for this form, we want a limit of 4 |
| 844 | + ChoiceFormSet = formset_factory(Choice, max_num=4) |
| 845 | + formset = ChoiceFormSet( |
| 846 | + { |
| 847 | + 'choices-TOTAL_FORMS': '4', |
| 848 | + 'choices-INITIAL_FORMS': '0', |
| 849 | + 'choices-MAX_NUM_FORMS': '4', |
| 850 | + 'choices-0-choice': 'Zero', |
| 851 | + 'choices-0-votes': '0', |
| 852 | + 'choices-1-choice': 'One', |
| 853 | + 'choices-1-votes': '1', |
| 854 | + 'choices-2-choice': 'Two', |
| 855 | + 'choices-2-votes': '2', |
| 856 | + 'choices-3-choice': 'Three', |
| 857 | + 'choices-3-votes': '3', |
| 858 | + }, |
| 859 | + prefix='choices', |
| 860 | + ) |
| 861 | + # This time four forms are instantiated |
| 862 | + self.assertEqual(len(formset.forms), 4) |
| 863 | + finally: |
| 864 | + formsets.DEFAULT_MAX_NUM = _old_DEFAULT_MAX_NUM |
| 865 | + |
| 866 | + |
808 | 867 | data = {
|
809 | 868 | 'choices-TOTAL_FORMS': '1', # the number of forms rendered
|
810 | 869 | 'choices-INITIAL_FORMS': '0', # the number of forms with initial data
|
@@ -900,12 +959,12 @@ def test_empty_forms_are_unbound(self):
|
900 | 959 | # The empty forms should be equal.
|
901 | 960 | self.assertEqual(empty_forms[0].as_p(), empty_forms[1].as_p())
|
902 | 961 |
|
903 |
| -class TestEmptyFormSet(TestCase): |
| 962 | +class TestEmptyFormSet(TestCase): |
904 | 963 | "Test that an empty formset still calls clean()"
|
905 |
| - def test_empty_formset_is_valid(self): |
906 |
| - EmptyFsetWontValidateFormset = formset_factory(FavoriteDrinkForm, extra=0, formset=EmptyFsetWontValidate) |
907 |
| - formset = EmptyFsetWontValidateFormset(data={'form-INITIAL_FORMS':'0', 'form-TOTAL_FORMS':'0'},prefix="form") |
908 |
| - formset2 = EmptyFsetWontValidateFormset(data={'form-INITIAL_FORMS':'0', 'form-TOTAL_FORMS':'1', 'form-0-name':'bah' },prefix="form") |
909 |
| - self.assertFalse(formset.is_valid()) |
910 |
| - self.assertFalse(formset2.is_valid()) |
| 964 | + def test_empty_formset_is_valid(self): |
| 965 | + EmptyFsetWontValidateFormset = formset_factory(FavoriteDrinkForm, extra=0, formset=EmptyFsetWontValidate) |
| 966 | + formset = EmptyFsetWontValidateFormset(data={'form-INITIAL_FORMS':'0', 'form-TOTAL_FORMS':'0'},prefix="form") |
| 967 | + formset2 = EmptyFsetWontValidateFormset(data={'form-INITIAL_FORMS':'0', 'form-TOTAL_FORMS':'1', 'form-0-name':'bah' },prefix="form") |
| 968 | + self.assertFalse(formset.is_valid()) |
| 969 | + self.assertFalse(formset2.is_valid()) |
911 | 970 |
|
0 commit comments