Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Made the semi-private _max_form_count live on the public API of forms…

…ets by renaming it to max_num. This also removes the ManagementForm use of MAX_COUNT since that usage should just be referenced to the formset's max_num property. Refs #7899. Thanks Peter of the Norse for straightening me out.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@8058 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 9d8f41baac6b3ca56c7732a2f93e15cda494a1ea 1 parent 62d9e27
Brian Rosner authored July 23, 2008
16  django/forms/formsets.py
@@ -10,7 +10,6 @@
10 10
 # special field names
11 11
 TOTAL_FORM_COUNT = 'TOTAL_FORMS'
12 12
 INITIAL_FORM_COUNT = 'INITIAL_FORMS'
13  
-MAX_FORM_COUNT = 'MAX_FORMS'
14 13
 ORDERING_FIELD_NAME = 'ORDER'
15 14
 DELETION_FIELD_NAME = 'DELETE'
16 15
 
@@ -23,7 +22,6 @@ class ManagementForm(Form):
23 22
     def __init__(self, *args, **kwargs):
24 23
         self.base_fields[TOTAL_FORM_COUNT] = IntegerField(widget=HiddenInput)
25 24
         self.base_fields[INITIAL_FORM_COUNT] = IntegerField(widget=HiddenInput)
26  
-        self.base_fields[MAX_FORM_COUNT] = IntegerField(widget=HiddenInput)
27 25
         super(ManagementForm, self).__init__(*args, **kwargs)
28 26
 
29 27
 class BaseFormSet(StrAndUnicode):
@@ -47,23 +45,21 @@ def __init__(self, data=None, files=None, auto_id='id_%s', prefix=None,
47 45
             if self.management_form.is_valid():
48 46
                 self._total_form_count = self.management_form.cleaned_data[TOTAL_FORM_COUNT]
49 47
                 self._initial_form_count = self.management_form.cleaned_data[INITIAL_FORM_COUNT]
50  
-                self._max_form_count = self.management_form.cleaned_data[MAX_FORM_COUNT]
51 48
             else:
52 49
                 raise ValidationError('ManagementForm data is missing or has been tampered with')
53 50
         else:
54 51
             if initial:
55 52
                 self._initial_form_count = len(initial)
56  
-                if self._initial_form_count > self._max_form_count and self._max_form_count > 0:
57  
-                    self._initial_form_count = self._max_form_count
  53
+                if self._initial_form_count > self.max_num and self.max_num > 0:
  54
+                    self._initial_form_count = self.max_num
58 55
                 self._total_form_count = self._initial_form_count + self.extra
59 56
             else:
60 57
                 self._initial_form_count = 0
61 58
                 self._total_form_count = self.extra
62  
-            if self._total_form_count > self._max_form_count and self._max_form_count > 0:
63  
-                self._total_form_count = self._max_form_count
  59
+            if self._total_form_count > self.max_num and self.max_num > 0:
  60
+                self._total_form_count = self.max_num
64 61
             initial = {TOTAL_FORM_COUNT: self._total_form_count,
65  
-                       INITIAL_FORM_COUNT: self._initial_form_count,
66  
-                       MAX_FORM_COUNT: self._max_form_count}
  62
+                       INITIAL_FORM_COUNT: self._initial_form_count}
67 63
             self.management_form = ManagementForm(initial=initial, auto_id=self.auto_id, prefix=self.prefix)
68 64
         
69 65
         # construct the forms in the formset
@@ -280,7 +276,7 @@ def formset_factory(form, formset=BaseFormSet, extra=1, can_order=False,
280 276
     """Return a FormSet for the given form class."""
281 277
     attrs = {'form': form, 'extra': extra,
282 278
              'can_order': can_order, 'can_delete': can_delete,
283  
-             '_max_form_count': max_num}
  279
+             'max_num': max_num}
284 280
     return type(form.__name__ + 'FormSet', (formset,), attrs)
285 281
 
286 282
 def all_valid(formsets):
4  django/forms/models.py
@@ -305,8 +305,8 @@ def __init__(self, data=None, files=None, auto_id='id_%s', prefix=None,
305 305
                  queryset=None, **kwargs):
306 306
         self.queryset = queryset
307 307
         defaults = {'data': data, 'files': files, 'auto_id': auto_id, 'prefix': prefix}
308  
-        if self._max_form_count > 0:
309  
-            qs = self.get_queryset()[:self._max_form_count]
  308
+        if self.max_num > 0:
  309
+            qs = self.get_queryset()[:self.max_num]
310 310
         else:
311 311
             qs = self.get_queryset()
312 312
         defaults['initial'] = [model_to_dict(obj) for obj in qs]
8  tests/modeltests/model_formsets/models.py
@@ -41,7 +41,6 @@ def __unicode__(self):
41 41
 >>> data = {
42 42
 ...     'form-TOTAL_FORMS': '3', # the number of forms rendered
43 43
 ...     'form-INITIAL_FORMS': '0', # the number of forms with initial data
44  
-...     'form-MAX_FORMS': '0', # the max number of forms
45 44
 ...     'form-0-name': 'Charles Baudelaire',
46 45
 ...     'form-1-name': 'Arthur Rimbaud',
47 46
 ...     'form-2-name': '',
@@ -79,7 +78,6 @@ def __unicode__(self):
79 78
 >>> data = {
80 79
 ...     'form-TOTAL_FORMS': '3', # the number of forms rendered
81 80
 ...     'form-INITIAL_FORMS': '2', # the number of forms with initial data
82  
-...     'form-MAX_FORMS': '0', # the max number of forms
83 81
 ...     'form-0-id': '2',
84 82
 ...     'form-0-name': 'Arthur Rimbaud',
85 83
 ...     'form-1-id': '1',
@@ -123,7 +121,6 @@ def __unicode__(self):
123 121
 >>> data = {
124 122
 ...     'form-TOTAL_FORMS': '4', # the number of forms rendered
125 123
 ...     'form-INITIAL_FORMS': '3', # the number of forms with initial data
126  
-...     'form-MAX_FORMS': '0', # the max number of forms
127 124
 ...     'form-0-id': '2',
128 125
 ...     'form-0-name': 'Arthur Rimbaud',
129 126
 ...     'form-1-id': '1',
@@ -153,7 +150,6 @@ def __unicode__(self):
153 150
 >>> data = {
154 151
 ...     'form-TOTAL_FORMS': '4', # the number of forms rendered
155 152
 ...     'form-INITIAL_FORMS': '3', # the number of forms with initial data
156  
-...     'form-MAX_FORMS': '0', # the max number of forms
157 153
 ...     'form-0-id': '2',
158 154
 ...     'form-0-name': 'Walt Whitman',
159 155
 ...     'form-1-id': '1',
@@ -184,7 +180,6 @@ def __unicode__(self):
184 180
 >>> data = {
185 181
 ...     'form-TOTAL_FORMS': '2', # the number of forms rendered
186 182
 ...     'form-INITIAL_FORMS': '1', # the number of forms with initial data
187  
-...     'form-MAX_FORMS': '0', # the max number of forms
188 183
 ...     'form-0-id': '1',
189 184
 ...     'form-0-name': '2nd Tuesday of the Week Meeting',
190 185
 ...     'form-0-authors': [2, 1, 3, 4],
@@ -242,7 +237,6 @@ def __unicode__(self):
242 237
 >>> data = {
243 238
 ...     'book_set-TOTAL_FORMS': '3', # the number of forms rendered
244 239
 ...     'book_set-INITIAL_FORMS': '0', # the number of forms with initial data
245  
-...     'book_set-MAX_FORMS': '0', # the max number of forms
246 240
 ...     'book_set-0-title': 'Les Fleurs du Mal',
247 241
 ...     'book_set-1-title': '',
248 242
 ...     'book_set-2-title': '',
@@ -277,7 +271,6 @@ def __unicode__(self):
277 271
 >>> data = {
278 272
 ...     'book_set-TOTAL_FORMS': '3', # the number of forms rendered
279 273
 ...     'book_set-INITIAL_FORMS': '1', # the number of forms with initial data
280  
-...     'book_set-MAX_FORMS': '0', # the max number of forms
281 274
 ...     'book_set-0-id': '1',
282 275
 ...     'book_set-0-title': 'Les Fleurs du Mal',
283 276
 ...     'book_set-1-title': 'Le Spleen de Paris',
@@ -304,7 +297,6 @@ def __unicode__(self):
304 297
 >>> data = {
305 298
 ...     'book_set-TOTAL_FORMS': '3', # the number of forms rendered
306 299
 ...     'book_set-INITIAL_FORMS': '2', # the number of forms with initial data
307  
-...     'book_set-MAX_FORMS': '0', # the max number of forms
308 300
 ...     'book_set-0-id': '1',
309 301
 ...     'book_set-0-title': 'Les Fleurs du Mal',
310 302
 ...     'book_set-1-id': '2',
16  tests/regressiontests/forms/formsets.py
@@ -20,7 +20,7 @@
20 20
 
21 21
 >>> formset = ChoiceFormSet(auto_id=False, prefix='choices')
22 22
 >>> print formset
23  
-<input type="hidden" name="choices-TOTAL_FORMS" value="1" /><input type="hidden" name="choices-INITIAL_FORMS" value="0" /><input type="hidden" name="choices-MAX_FORMS" value="0" />
  23
+<input type="hidden" name="choices-TOTAL_FORMS" value="1" /><input type="hidden" name="choices-INITIAL_FORMS" value="0" />
24 24
 <tr><th>Choice:</th><td><input type="text" name="choices-0-choice" /></td></tr>
25 25
 <tr><th>Votes:</th><td><input type="text" name="choices-0-votes" /></td></tr>
26 26
 
@@ -34,7 +34,6 @@
34 34
 >>> data = {
35 35
 ...     'choices-TOTAL_FORMS': '1', # the number of forms rendered
36 36
 ...     'choices-INITIAL_FORMS': '0', # the number of forms with initial data
37  
-...     'choices-MAX_FORMS': '0', # the max number of forms
38 37
 ...     'choices-0-choice': 'Calexico',
39 38
 ...     'choices-0-votes': '100',
40 39
 ... }
@@ -61,7 +60,6 @@
61 60
 >>> data = {
62 61
 ...     'choices-TOTAL_FORMS': '1', # the number of forms rendered
63 62
 ...     'choices-INITIAL_FORMS': '0', # the number of forms with initial data
64  
-...     'choices-MAX_FORMS': '0', # the max number of forms
65 63
 ...     'choices-0-choice': 'Calexico',
66 64
 ...     'choices-0-votes': '',
67 65
 ... }
@@ -92,7 +90,6 @@
92 90
 >>> data = {
93 91
 ...     'choices-TOTAL_FORMS': '2', # the number of forms rendered
94 92
 ...     'choices-INITIAL_FORMS': '1', # the number of forms with initial data
95  
-...     'choices-MAX_FORMS': '0', # the max number of forms
96 93
 ...     'choices-0-choice': 'Calexico',
97 94
 ...     'choices-0-votes': '100',
98 95
 ...     'choices-1-choice': '',
@@ -114,7 +111,6 @@
114 111
 >>> data = {
115 112
 ...     'choices-TOTAL_FORMS': '2', # the number of forms rendered
116 113
 ...     'choices-INITIAL_FORMS': '1', # the number of forms with initial data
117  
-...     'choices-MAX_FORMS': '0', # the max number of forms
118 114
 ...     'choices-0-choice': 'Calexico',
119 115
 ...     'choices-0-votes': '100',
120 116
 ...     'choices-1-choice': 'The Decemberists',
@@ -134,7 +130,6 @@
134 130
 >>> data = {
135 131
 ...     'choices-TOTAL_FORMS': '2', # the number of forms rendered
136 132
 ...     'choices-INITIAL_FORMS': '1', # the number of forms with initial data
137  
-...     'choices-MAX_FORMS': '0', # the max number of forms
138 133
 ...     'choices-0-choice': '', # deleted value
139 134
 ...     'choices-0-votes': '', # deleted value
140 135
 ...     'choices-1-choice': '',
@@ -172,7 +167,6 @@
172 167
 >>> data = {
173 168
 ...     'choices-TOTAL_FORMS': '3', # the number of forms rendered
174 169
 ...     'choices-INITIAL_FORMS': '0', # the number of forms with initial data
175  
-...     'choices-MAX_FORMS': '0', # the max number of forms
176 170
 ...     'choices-0-choice': '',
177 171
 ...     'choices-0-votes': '',
178 172
 ...     'choices-1-choice': '',
@@ -193,7 +187,6 @@
193 187
 >>> data = {
194 188
 ...     'choices-TOTAL_FORMS': '3', # the number of forms rendered
195 189
 ...     'choices-INITIAL_FORMS': '0', # the number of forms with initial data
196  
-...     'choices-MAX_FORMS': '0', # the max number of forms
197 190
 ...     'choices-0-choice': 'Calexico',
198 191
 ...     'choices-0-votes': '100',
199 192
 ...     'choices-1-choice': '',
@@ -214,7 +207,6 @@
214 207
 >>> data = {
215 208
 ...     'choices-TOTAL_FORMS': '3', # the number of forms rendered
216 209
 ...     'choices-INITIAL_FORMS': '0', # the number of forms with initial data
217  
-...     'choices-MAX_FORMS': '0', # the max number of forms
218 210
 ...     'choices-0-choice': 'Calexico',
219 211
 ...     'choices-0-votes': '100',
220 212
 ...     'choices-1-choice': 'The Decemberists',
@@ -275,7 +267,6 @@
275 267
 >>> data = {
276 268
 ...     'choices-TOTAL_FORMS': '3', # the number of forms rendered
277 269
 ...     'choices-INITIAL_FORMS': '2', # the number of forms with initial data
278  
-...     'choices-MAX_FORMS': '0', # the max number of forms
279 270
 ...     'choices-0-choice': 'Calexico',
280 271
 ...     'choices-0-votes': '100',
281 272
 ...     'choices-0-DELETE': '',
@@ -325,7 +316,6 @@
325 316
 >>> data = {
326 317
 ...     'choices-TOTAL_FORMS': '3', # the number of forms rendered
327 318
 ...     'choices-INITIAL_FORMS': '2', # the number of forms with initial data
328  
-...     'choices-MAX_FORMS': '0', # the max number of forms
329 319
 ...     'choices-0-choice': 'Calexico',
330 320
 ...     'choices-0-votes': '100',
331 321
 ...     'choices-0-ORDER': '1',
@@ -352,7 +342,6 @@
352 342
 >>> data = {
353 343
 ...     'choices-TOTAL_FORMS': '4', # the number of forms rendered
354 344
 ...     'choices-INITIAL_FORMS': '3', # the number of forms with initial data
355  
-...     'choices-MAX_FORMS': '0', # the max number of forms
356 345
 ...     'choices-0-choice': 'Calexico',
357 346
 ...     'choices-0-votes': '100',
358 347
 ...     'choices-0-ORDER': '1',
@@ -414,7 +403,6 @@
414 403
 >>> data = {
415 404
 ...     'choices-TOTAL_FORMS': '4', # the number of forms rendered
416 405
 ...     'choices-INITIAL_FORMS': '3', # the number of forms with initial data
417  
-...     'choices-MAX_FORMS': '0', # the max number of forms
418 406
 ...     'choices-0-choice': 'Calexico',
419 407
 ...     'choices-0-votes': '100',
420 408
 ...     'choices-0-ORDER': '1',
@@ -473,7 +461,6 @@
473 461
 >>> data = {
474 462
 ...     'drinks-TOTAL_FORMS': '2', # the number of forms rendered
475 463
 ...     'drinks-INITIAL_FORMS': '0', # the number of forms with initial data
476  
-...     'drinks-MAX_FORMS': '0', # the max number of forms
477 464
 ...     'drinks-0-name': 'Gin and Tonic',
478 465
 ...     'drinks-1-name': 'Gin and Tonic',
479 466
 ... }
@@ -495,7 +482,6 @@
495 482
 >>> data = {
496 483
 ...     'drinks-TOTAL_FORMS': '2', # the number of forms rendered
497 484
 ...     'drinks-INITIAL_FORMS': '0', # the number of forms with initial data
498  
-...     'drinks-MAX_FORMS': '0', # the max number of forms
499 485
 ...     'drinks-0-name': 'Gin and Tonic',
500 486
 ...     'drinks-1-name': 'Bloody Mary',
501 487
 ... }

0 notes on commit 9d8f41b

Please sign in to comment.
Something went wrong with that request. Please try again.