Ticket #14580: 14580.diff

File 14580.diff, 5.8 KB (added by Tim Graham, 11 years ago)
  • django/contrib/admin/options.py

    diff --git a/django/contrib/admin/options.py b/django/contrib/admin/options.py
    index 2c060a0..0b941df 100644
    a b class ModelAdmin(BaseModelAdmin):  
    11071107            else:
    11081108                form_validated = False
    11091109                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)
    11211111            if all_valid(formsets) and form_validated:
    11221112                self.save_model(request, new_object, form, False)
    11231113                self.save_related(request, form, formsets, False)
    class ModelAdmin(BaseModelAdmin):  
    11351125                if isinstance(f, models.ManyToManyField):
    11361126                    initial[k] = initial[k].split(",")
    11371127            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)
    11471129
    11481130        adminForm = helpers.AdminForm(form, list(self.get_fieldsets(request)),
    11491131            self.get_prepopulated_fields(request),
    class ModelAdmin(BaseModelAdmin):  
    11951177                                    current_app=self.admin_site.name))
    11961178
    11971179        ModelForm = self.get_form(request, obj)
    1198         formsets = []
    11991180        inline_instances = self.get_inline_instances(request, obj)
    12001181        if request.method == 'POST':
    12011182            form = ModelForm(request.POST, request.FILES, instance=obj)
    class ModelAdmin(BaseModelAdmin):  
    12051186            else:
    12061187                form_validated = False
    12071188                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)
    12201190            if all_valid(formsets) and form_validated:
    12211191                self.save_model(request, new_object, form, True)
    12221192                self.save_related(request, form, formsets, True)
    class ModelAdmin(BaseModelAdmin):  
    12261196
    12271197        else:
    12281198            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)
    12381200
    12391201        adminForm = helpers.AdminForm(form, self.get_fieldsets(request, obj),
    12401202            self.get_prepopulated_fields(request, obj),
    class ModelAdmin(BaseModelAdmin):  
    15331495            "admin/object_history.html"
    15341496        ], context, current_app=self.admin_site.name)
    15351497
     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
    15361524
    15371525class InlineModelAdmin(BaseModelAdmin):
    15381526    """
Back to Top