Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #14850 -- Cleaned up duplicate code in admin formset handling.

Thanks apollo13 for the report and review.
  • Loading branch information...
commit 402b4a7a20a4f00fce0f01cdc3f5f97967fdb935 1 parent e4937b6
Tim Graham authored July 29, 2013

Showing 1 changed file with 30 additions and 42 deletions. Show diff stats Hide diff stats

  1. 72  django/contrib/admin/options.py
72  django/contrib/admin/options.py
@@ -1107,17 +1107,7 @@ def add_view(self, request, form_url='', extra_context=None):
1107 1107
             else:
1108 1108
                 form_validated = False
1109 1109
                 new_object = self.model()
1110  
-            prefixes = {}
1111  
-            for FormSet, inline in zip(self.get_formsets(request), inline_instances):
1112  
-                prefix = FormSet.get_default_prefix()
1113  
-                prefixes[prefix] = prefixes.get(prefix, 0) + 1
1114  
-                if prefixes[prefix] != 1 or not prefix:
1115  
-                    prefix = "%s-%s" % (prefix, prefixes[prefix])
1116  
-                formset = FormSet(data=request.POST, files=request.FILES,
1117  
-                                  instance=new_object,
1118  
-                                  save_as_new="_saveasnew" in request.POST,
1119  
-                                  prefix=prefix, queryset=inline.get_queryset(request))
1120  
-                formsets.append(formset)
  1110
+            formsets = self._create_formsets(request, new_object, inline_instances)
1121 1111
             if all_valid(formsets) and form_validated:
1122 1112
                 self.save_model(request, new_object, form, False)
1123 1113
                 self.save_related(request, form, formsets, False)
@@ -1135,15 +1125,7 @@ def add_view(self, request, form_url='', extra_context=None):
1135 1125
                 if isinstance(f, models.ManyToManyField):
1136 1126
                     initial[k] = initial[k].split(",")
1137 1127
             form = ModelForm(initial=initial)
1138  
-            prefixes = {}
1139  
-            for FormSet, inline in zip(self.get_formsets(request), inline_instances):
1140  
-                prefix = FormSet.get_default_prefix()
1141  
-                prefixes[prefix] = prefixes.get(prefix, 0) + 1
1142  
-                if prefixes[prefix] != 1 or not prefix:
1143  
-                    prefix = "%s-%s" % (prefix, prefixes[prefix])
1144  
-                formset = FormSet(instance=self.model(), prefix=prefix,
1145  
-                                  queryset=inline.get_queryset(request))
1146  
-                formsets.append(formset)
  1128
+            formsets = self._create_formsets(request, self.model(), inline_instances)
1147 1129
 
1148 1130
         adminForm = helpers.AdminForm(form, list(self.get_fieldsets(request)),
1149 1131
             self.get_prepopulated_fields(request),
@@ -1195,7 +1177,6 @@ def change_view(self, request, object_id, form_url='', extra_context=None):
1195 1177
                                     current_app=self.admin_site.name))
1196 1178
 
1197 1179
         ModelForm = self.get_form(request, obj)
1198  
-        formsets = []
1199 1180
         inline_instances = self.get_inline_instances(request, obj)
1200 1181
         if request.method == 'POST':
1201 1182
             form = ModelForm(request.POST, request.FILES, instance=obj)
@@ -1205,18 +1186,7 @@ def change_view(self, request, object_id, form_url='', extra_context=None):
1205 1186
             else:
1206 1187
                 form_validated = False
1207 1188
                 new_object = obj
1208  
-            prefixes = {}
1209  
-            for FormSet, inline in zip(self.get_formsets(request, new_object), inline_instances):
1210  
-                prefix = FormSet.get_default_prefix()
1211  
-                prefixes[prefix] = prefixes.get(prefix, 0) + 1
1212  
-                if prefixes[prefix] != 1 or not prefix:
1213  
-                    prefix = "%s-%s" % (prefix, prefixes[prefix])
1214  
-                formset = FormSet(request.POST, request.FILES,
1215  
-                                  instance=new_object, prefix=prefix,
1216  
-                                  queryset=inline.get_queryset(request))
1217  
-
1218  
-                formsets.append(formset)
1219  
-
  1189
+            formsets = self._create_formsets(request, new_object, inline_instances)
1220 1190
             if all_valid(formsets) and form_validated:
1221 1191
                 self.save_model(request, new_object, form, True)
1222 1192
                 self.save_related(request, form, formsets, True)
@@ -1226,15 +1196,7 @@ def change_view(self, request, object_id, form_url='', extra_context=None):
1226 1196
 
1227 1197
         else:
1228 1198
             form = ModelForm(instance=obj)
1229  
-            prefixes = {}
1230  
-            for FormSet, inline in zip(self.get_formsets(request, obj), inline_instances):
1231  
-                prefix = FormSet.get_default_prefix()
1232  
-                prefixes[prefix] = prefixes.get(prefix, 0) + 1
1233  
-                if prefixes[prefix] != 1 or not prefix:
1234  
-                    prefix = "%s-%s" % (prefix, prefixes[prefix])
1235  
-                formset = FormSet(instance=obj, prefix=prefix,
1236  
-                                  queryset=inline.get_queryset(request))
1237  
-                formsets.append(formset)
  1199
+            formsets = self._create_formsets(request, obj, inline_instances)
1238 1200
 
1239 1201
         adminForm = helpers.AdminForm(form, self.get_fieldsets(request, obj),
1240 1202
             self.get_prepopulated_fields(request, obj),
@@ -1533,6 +1495,32 @@ def history_view(self, request, object_id, extra_context=None):
1533 1495
             "admin/object_history.html"
1534 1496
         ], context, current_app=self.admin_site.name)
1535 1497
 
  1498
+    def _create_formsets(self, request, obj, inline_instances):
  1499
+        "Helper function to generate formsets for add/change_view."
  1500
+        formsets = []
  1501
+        prefixes = {}
  1502
+        get_formsets_args = [request]
  1503
+        if obj.pk:
  1504
+            get_formsets_args.append(obj)
  1505
+        for FormSet, inline in zip(self.get_formsets(*get_formsets_args), inline_instances):
  1506
+            prefix = FormSet.get_default_prefix()
  1507
+            prefixes[prefix] = prefixes.get(prefix, 0) + 1
  1508
+            if prefixes[prefix] != 1 or not prefix:
  1509
+                prefix = "%s-%s" % (prefix, prefixes[prefix])
  1510
+            formset_params = {
  1511
+                'instance': obj,
  1512
+                'prefix': prefix,
  1513
+                'queryset': inline.get_queryset(request),
  1514
+            }
  1515
+            if request.method == 'POST':
  1516
+                formset_params.update({
  1517
+                    'data': request.POST,
  1518
+                    'files': request.FILES,
  1519
+                    'save_as_new': '_saveasnew' in request.POST
  1520
+                })
  1521
+            formsets.append(FormSet(**formset_params))
  1522
+        return formsets
  1523
+
1536 1524
 
1537 1525
 class InlineModelAdmin(BaseModelAdmin):
1538 1526
     """

0 notes on commit 402b4a7

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