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
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>""")
650
650
# Limiting the maximum number of forms ########################################
651
651
# Base case for max_num.
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.
656
656
LimitedFavoriteDrinkFormSet = formset_factory(FavoriteDrinkForm, extra=3)
657
657
formset = LimitedFavoriteDrinkFormSet()
698
698
def test_max_num_with_initial_data(self):
699
699
# max_num with initial data
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
844
844
self.assertEqual(len(formset.forms), 0)
845
845
self.assertTrue(formset)
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
852
formsets.DEFAULT_MAX_NUM = 3
853
ChoiceFormSet = formset_factory(Choice)
854
# someone fiddles with the mgmt form data...
855
formset = ChoiceFormSet(
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',
871
# But we still only instantiate 3 forms
872
self.assertEqual(len(formset.forms), 3)
874
formsets.DEFAULT_MAX_NUM = _old_DEFAULT_MAX_NUM
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
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(
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',
900
# This time four forms are instantiated
901
self.assertEqual(len(formset.forms), 4)
903
formsets.DEFAULT_MAX_NUM = _old_DEFAULT_MAX_NUM
849
907
'choices-TOTAL_FORMS': '1', # the number of forms rendered