From a77e541d1d8766b0e7d2c4143076ef07973f5bd9 Mon Sep 17 00:00:00 2001 From: Adam Johnson Date: Sat, 10 Jan 2026 20:38:56 +0000 Subject: Fixed #36801 -- Avoided unnecessary calculation in construct_change_message(). MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `changed_field_labels` is only needed if there are changes to log, so move its calculation, including the somewhat costly `translation_override()`, inside the conditional that checks for changes. Also avoid reading `form.changed_data` when it’s already bound to `changed_data`. co-authored-by: Rodolfo Becerra <44782644+rodolvbg@users.noreply.github.com> --- django/contrib/admin/utils.py | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/django/contrib/admin/utils.py b/django/contrib/admin/utils.py index 8263b6f9e2..e21a6102b5 100644 --- a/django/contrib/admin/utils.py +++ b/django/contrib/admin/utils.py @@ -552,21 +552,21 @@ def construct_change_message(form, formsets, add): Translations are deactivated so that strings are stored untranslated. Translation happens later on LogEntry access. """ + change_message = [] + if add: + change_message.append({"added": {}}) # Evaluating `form.changed_data` prior to disabling translations is # required to avoid fields affected by localization from being included # incorrectly, e.g. where date formats differ such as MM/DD/YYYY vs # DD/MM/YYYY. - changed_data = form.changed_data - with translation_override(None): - # Deactivate translations while fetching verbose_name for form - # field labels and using `field_name`, if verbose_name is not provided. - # Translations will happen later on LogEntry access. - changed_field_labels = _get_changed_field_labels_from_form(form, changed_data) - - change_message = [] - if add: - change_message.append({"added": {}}) - elif form.changed_data: + elif changed_data := form.changed_data: + with translation_override(None): + # Deactivate translations while fetching verbose_name for form + # field labels and using `field_name`, if verbose_name is not + # provided. Translations will happen later on LogEntry access. + changed_field_labels = _get_changed_field_labels_from_form( + form, changed_data + ) change_message.append({"changed": {"fields": changed_field_labels}}) if formsets: with translation_override(None): -- cgit v1.3