Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

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
@timgraham timgraham authored
Showing with 30 additions and 42 deletions.
  1. +30 −42 django/contrib/admin/options.py
View
72 django/contrib/admin/options.py
@@ -1107,17 +1107,7 @@ def add_view(self, request, form_url='', extra_context=None):
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 @@ def add_view(self, request, form_url='', extra_context=None):
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 @@ def change_view(self, request, object_id, form_url='', extra_context=None):
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 @@ def change_view(self, request, object_id, form_url='', extra_context=None):
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 @@ def change_view(self, request, object_id, form_url='', extra_context=None):
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 @@ def history_view(self, request, object_id, extra_context=None):
"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):
"""
Please sign in to comment.
Something went wrong with that request. Please try again.