Fixed #14850 -- Cleaned up duplicate code in admin formset handling.
Thanks apollo13 for the report and review.
This commit is contained in:
parent
e4937b6dde
commit
402b4a7a20
|
@ -1107,17 +1107,7 @@ class ModelAdmin(BaseModelAdmin):
|
||||||
else:
|
else:
|
||||||
form_validated = False
|
form_validated = False
|
||||||
new_object = self.model()
|
new_object = self.model()
|
||||||
prefixes = {}
|
formsets = self._create_formsets(request, new_object, inline_instances)
|
||||||
for FormSet, inline in zip(self.get_formsets(request), inline_instances):
|
|
||||||
prefix = FormSet.get_default_prefix()
|
|
||||||
prefixes[prefix] = prefixes.get(prefix, 0) + 1
|
|
||||||
if prefixes[prefix] != 1 or not prefix:
|
|
||||||
prefix = "%s-%s" % (prefix, prefixes[prefix])
|
|
||||||
formset = FormSet(data=request.POST, files=request.FILES,
|
|
||||||
instance=new_object,
|
|
||||||
save_as_new="_saveasnew" in request.POST,
|
|
||||||
prefix=prefix, queryset=inline.get_queryset(request))
|
|
||||||
formsets.append(formset)
|
|
||||||
if all_valid(formsets) and form_validated:
|
if all_valid(formsets) and form_validated:
|
||||||
self.save_model(request, new_object, form, False)
|
self.save_model(request, new_object, form, False)
|
||||||
self.save_related(request, form, formsets, False)
|
self.save_related(request, form, formsets, False)
|
||||||
|
@ -1135,15 +1125,7 @@ class ModelAdmin(BaseModelAdmin):
|
||||||
if isinstance(f, models.ManyToManyField):
|
if isinstance(f, models.ManyToManyField):
|
||||||
initial[k] = initial[k].split(",")
|
initial[k] = initial[k].split(",")
|
||||||
form = ModelForm(initial=initial)
|
form = ModelForm(initial=initial)
|
||||||
prefixes = {}
|
formsets = self._create_formsets(request, self.model(), inline_instances)
|
||||||
for FormSet, inline in zip(self.get_formsets(request), inline_instances):
|
|
||||||
prefix = FormSet.get_default_prefix()
|
|
||||||
prefixes[prefix] = prefixes.get(prefix, 0) + 1
|
|
||||||
if prefixes[prefix] != 1 or not prefix:
|
|
||||||
prefix = "%s-%s" % (prefix, prefixes[prefix])
|
|
||||||
formset = FormSet(instance=self.model(), prefix=prefix,
|
|
||||||
queryset=inline.get_queryset(request))
|
|
||||||
formsets.append(formset)
|
|
||||||
|
|
||||||
adminForm = helpers.AdminForm(form, list(self.get_fieldsets(request)),
|
adminForm = helpers.AdminForm(form, list(self.get_fieldsets(request)),
|
||||||
self.get_prepopulated_fields(request),
|
self.get_prepopulated_fields(request),
|
||||||
|
@ -1195,7 +1177,6 @@ class ModelAdmin(BaseModelAdmin):
|
||||||
current_app=self.admin_site.name))
|
current_app=self.admin_site.name))
|
||||||
|
|
||||||
ModelForm = self.get_form(request, obj)
|
ModelForm = self.get_form(request, obj)
|
||||||
formsets = []
|
|
||||||
inline_instances = self.get_inline_instances(request, obj)
|
inline_instances = self.get_inline_instances(request, obj)
|
||||||
if request.method == 'POST':
|
if request.method == 'POST':
|
||||||
form = ModelForm(request.POST, request.FILES, instance=obj)
|
form = ModelForm(request.POST, request.FILES, instance=obj)
|
||||||
|
@ -1205,18 +1186,7 @@ class ModelAdmin(BaseModelAdmin):
|
||||||
else:
|
else:
|
||||||
form_validated = False
|
form_validated = False
|
||||||
new_object = obj
|
new_object = obj
|
||||||
prefixes = {}
|
formsets = self._create_formsets(request, new_object, inline_instances)
|
||||||
for FormSet, inline in zip(self.get_formsets(request, new_object), inline_instances):
|
|
||||||
prefix = FormSet.get_default_prefix()
|
|
||||||
prefixes[prefix] = prefixes.get(prefix, 0) + 1
|
|
||||||
if prefixes[prefix] != 1 or not prefix:
|
|
||||||
prefix = "%s-%s" % (prefix, prefixes[prefix])
|
|
||||||
formset = FormSet(request.POST, request.FILES,
|
|
||||||
instance=new_object, prefix=prefix,
|
|
||||||
queryset=inline.get_queryset(request))
|
|
||||||
|
|
||||||
formsets.append(formset)
|
|
||||||
|
|
||||||
if all_valid(formsets) and form_validated:
|
if all_valid(formsets) and form_validated:
|
||||||
self.save_model(request, new_object, form, True)
|
self.save_model(request, new_object, form, True)
|
||||||
self.save_related(request, form, formsets, True)
|
self.save_related(request, form, formsets, True)
|
||||||
|
@ -1226,15 +1196,7 @@ class ModelAdmin(BaseModelAdmin):
|
||||||
|
|
||||||
else:
|
else:
|
||||||
form = ModelForm(instance=obj)
|
form = ModelForm(instance=obj)
|
||||||
prefixes = {}
|
formsets = self._create_formsets(request, obj, inline_instances)
|
||||||
for FormSet, inline in zip(self.get_formsets(request, obj), inline_instances):
|
|
||||||
prefix = FormSet.get_default_prefix()
|
|
||||||
prefixes[prefix] = prefixes.get(prefix, 0) + 1
|
|
||||||
if prefixes[prefix] != 1 or not prefix:
|
|
||||||
prefix = "%s-%s" % (prefix, prefixes[prefix])
|
|
||||||
formset = FormSet(instance=obj, prefix=prefix,
|
|
||||||
queryset=inline.get_queryset(request))
|
|
||||||
formsets.append(formset)
|
|
||||||
|
|
||||||
adminForm = helpers.AdminForm(form, self.get_fieldsets(request, obj),
|
adminForm = helpers.AdminForm(form, self.get_fieldsets(request, obj),
|
||||||
self.get_prepopulated_fields(request, obj),
|
self.get_prepopulated_fields(request, obj),
|
||||||
|
@ -1533,6 +1495,32 @@ class ModelAdmin(BaseModelAdmin):
|
||||||
"admin/object_history.html"
|
"admin/object_history.html"
|
||||||
], context, current_app=self.admin_site.name)
|
], context, current_app=self.admin_site.name)
|
||||||
|
|
||||||
|
def _create_formsets(self, request, obj, inline_instances):
|
||||||
|
"Helper function to generate formsets for add/change_view."
|
||||||
|
formsets = []
|
||||||
|
prefixes = {}
|
||||||
|
get_formsets_args = [request]
|
||||||
|
if obj.pk:
|
||||||
|
get_formsets_args.append(obj)
|
||||||
|
for FormSet, inline in zip(self.get_formsets(*get_formsets_args), inline_instances):
|
||||||
|
prefix = FormSet.get_default_prefix()
|
||||||
|
prefixes[prefix] = prefixes.get(prefix, 0) + 1
|
||||||
|
if prefixes[prefix] != 1 or not prefix:
|
||||||
|
prefix = "%s-%s" % (prefix, prefixes[prefix])
|
||||||
|
formset_params = {
|
||||||
|
'instance': obj,
|
||||||
|
'prefix': prefix,
|
||||||
|
'queryset': inline.get_queryset(request),
|
||||||
|
}
|
||||||
|
if request.method == 'POST':
|
||||||
|
formset_params.update({
|
||||||
|
'data': request.POST,
|
||||||
|
'files': request.FILES,
|
||||||
|
'save_as_new': '_saveasnew' in request.POST
|
||||||
|
})
|
||||||
|
formsets.append(FormSet(**formset_params))
|
||||||
|
return formsets
|
||||||
|
|
||||||
|
|
||||||
class InlineModelAdmin(BaseModelAdmin):
|
class InlineModelAdmin(BaseModelAdmin):
|
||||||
"""
|
"""
|
||||||
|
|
Loading…
Reference in New Issue