Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #10082 -- Modified BaseFormSet so that ordering checks work whe…

…n the formset is empty. Thanks to Petr Marhoun for the report and test case, and bmathieu for the fix.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@10643 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 2ba9df2f41cd649b42f5d7452aa2393aa6131f32 1 parent 0c1d38b
Russell Keith-Magee authored April 28, 2009
20  django/forms/formsets.py
@@ -170,18 +170,18 @@ def _get_ordered_forms(self):
170 170
                 # don't add data marked for deletion to self.ordered_data
171 171
                 if self.can_delete and form.cleaned_data[DELETION_FIELD_NAME]:
172 172
                     continue
173  
-                # A sort function to order things numerically ascending, but
174  
-                # None should be sorted below anything else. Allowing None as
175  
-                # a comparison value makes it so we can leave ordering fields
176  
-                # blamk.
177  
-                def compare_ordering_values(x, y):
178  
-                    if x[1] is None:
179  
-                        return 1
180  
-                    if y[1] is None:
181  
-                        return -1
182  
-                    return x[1] - y[1]
183 173
                 self._ordering.append((i, form.cleaned_data[ORDERING_FIELD_NAME]))
184 174
             # After we're done populating self._ordering, sort it.
  175
+            # A sort function to order things numerically ascending, but
  176
+            # None should be sorted below anything else. Allowing None as
  177
+            # a comparison value makes it so we can leave ordering fields
  178
+            # blamk.
  179
+            def compare_ordering_values(x, y):
  180
+                if x[1] is None:
  181
+                    return 1
  182
+                if y[1] is None:
  183
+                    return -1
  184
+                return x[1] - y[1]
185 185
             self._ordering.sort(compare_ordering_values)
186 186
         # Return a list of form.cleaned_data dicts in the order spcified by
187 187
         # the form data.
12  tests/regressiontests/forms/formsets.py
@@ -394,6 +394,18 @@
394 394
 {'votes': 500, 'ORDER': None, 'choice': u'The Decemberists'}
395 395
 {'votes': 50, 'ORDER': None, 'choice': u'Basia Bulat'}
396 396
 
  397
+Ordering should work with blank fieldsets.
  398
+
  399
+>>> data = {
  400
+...     'choices-TOTAL_FORMS': '3', # the number of forms rendered
  401
+...     'choices-INITIAL_FORMS': '0', # the number of forms with initial data
  402
+... }
  403
+
  404
+>>> formset = ChoiceFormSet(data, auto_id=False, prefix='choices')
  405
+>>> formset.is_valid()
  406
+True
  407
+>>> for form in formset.ordered_forms:
  408
+...    print form.cleaned_data
397 409
 
398 410
 # FormSets with ordering + deletion ###########################################
399 411
 

0 notes on commit 2ba9df2

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