mirror of https://github.com/django/django.git
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:
|
||||
form_validated = False
|
||||
new_object = self.model()
|
||||
prefixes = {}
|
||||
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)
|
||||
formsets = self._create_formsets(request, new_object, inline_instances)
|
||||
if all_valid(formsets) and form_validated:
|
||||
self.save_model(request, new_object, form, False)
|
||||
self.save_related(request, form, formsets, False)
|
||||
|
@ -1135,15 +1125,7 @@ class ModelAdmin(BaseModelAdmin):
|
|||
if isinstance(f, models.ManyToManyField):
|
||||
initial[k] = initial[k].split(",")
|
||||
form = ModelForm(initial=initial)
|
||||
prefixes = {}
|
||||
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)
|
||||
formsets = self._create_formsets(request, self.model(), inline_instances)
|
||||
|
||||
adminForm = helpers.AdminForm(form, list(self.get_fieldsets(request)),
|
||||
self.get_prepopulated_fields(request),
|
||||
|
@ -1195,7 +1177,6 @@ class ModelAdmin(BaseModelAdmin):
|
|||
current_app=self.admin_site.name))
|
||||
|
||||
ModelForm = self.get_form(request, obj)
|
||||
formsets = []
|
||||
inline_instances = self.get_inline_instances(request, obj)
|
||||
if request.method == 'POST':
|
||||
form = ModelForm(request.POST, request.FILES, instance=obj)
|
||||
|
@ -1205,18 +1186,7 @@ class ModelAdmin(BaseModelAdmin):
|
|||
else:
|
||||
form_validated = False
|
||||
new_object = obj
|
||||
prefixes = {}
|
||||
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)
|
||||
|
||||
formsets = self._create_formsets(request, new_object, inline_instances)
|
||||
if all_valid(formsets) and form_validated:
|
||||
self.save_model(request, new_object, form, True)
|
||||
self.save_related(request, form, formsets, True)
|
||||
|
@ -1226,15 +1196,7 @@ class ModelAdmin(BaseModelAdmin):
|
|||
|
||||
else:
|
||||
form = ModelForm(instance=obj)
|
||||
prefixes = {}
|
||||
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)
|
||||
formsets = self._create_formsets(request, obj, inline_instances)
|
||||
|
||||
adminForm = helpers.AdminForm(form, self.get_fieldsets(request, obj),
|
||||
self.get_prepopulated_fields(request, obj),
|
||||
|
@ -1533,6 +1495,32 @@ class ModelAdmin(BaseModelAdmin):
|
|||
"admin/object_history.html"
|
||||
], 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):
|
||||
"""
|
||||
|
|
Loading…
Reference in New Issue