From 68e018010befe78b937b0a6cf045f1a12ed26fca Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Fri, 17 Jan 2020 16:00:04 +0100 Subject: [PATCH] Optimized ModelAdmin._changeform_view() by avoiding multiple get_fieldsets() calls. Co-authored-by: Hasan Ramezani --- django/contrib/admin/options.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/django/contrib/admin/options.py b/django/contrib/admin/options.py index 795d20f96ac..a0429a0e843 100644 --- a/django/contrib/admin/options.py +++ b/django/contrib/admin/options.py @@ -1552,7 +1552,10 @@ class ModelAdmin(BaseModelAdmin): if obj is None: return self._get_obj_does_not_exist_redirect(request, opts, object_id) - ModelForm = self.get_form(request, obj, change=not add) + fieldsets = self.get_fieldsets(request, obj) + ModelForm = self.get_form( + request, obj, change=not add, fields=flatten_fieldsets(fieldsets) + ) if request.method == 'POST': form = ModelForm(request.POST, request.FILES, instance=obj) form_validated = form.is_valid() @@ -1583,12 +1586,12 @@ class ModelAdmin(BaseModelAdmin): formsets, inline_instances = self._create_formsets(request, obj, change=True) if not add and not self.has_change_permission(request, obj): - readonly_fields = flatten_fieldsets(self.get_fieldsets(request, obj)) + readonly_fields = flatten_fieldsets(fieldsets) else: readonly_fields = self.get_readonly_fields(request, obj) adminForm = helpers.AdminForm( form, - list(self.get_fieldsets(request, obj)), + list(fieldsets), # Clear prepopulated fields on a view-only form to avoid a crash. self.get_prepopulated_fields(request, obj) if add or self.has_change_permission(request, obj) else {}, readonly_fields,