summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdam Johnson <me@adamj.eu>2026-01-10 20:38:56 +0000
committerJacob Walls <jacobtylerwalls@gmail.com>2026-01-16 09:17:57 -0500
commita77e541d1d8766b0e7d2c4143076ef07973f5bd9 (patch)
treeef572eb99dc6c1c81a35e030764c8a15976add81
parent211b63142786ea77953de4f2850961e259c5b335 (diff)
Fixed #36801 -- Avoided unnecessary calculation in construct_change_message().
`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>
-rw-r--r--django/contrib/admin/utils.py22
1 files 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):