Skip to content

Commit a77e541

Browse files
adamchainzrodolvbg
authored andcommitted
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>
1 parent 211b631 commit a77e541

1 file changed

Lines changed: 11 additions & 11 deletions

File tree

django/contrib/admin/utils.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -552,21 +552,21 @@ def construct_change_message(form, formsets, add):
552552
Translations are deactivated so that strings are stored untranslated.
553553
Translation happens later on LogEntry access.
554554
"""
555+
change_message = []
556+
if add:
557+
change_message.append({"added": {}})
555558
# Evaluating `form.changed_data` prior to disabling translations is
556559
# required to avoid fields affected by localization from being included
557560
# incorrectly, e.g. where date formats differ such as MM/DD/YYYY vs
558561
# DD/MM/YYYY.
559-
changed_data = form.changed_data
560-
with translation_override(None):
561-
# Deactivate translations while fetching verbose_name for form
562-
# field labels and using `field_name`, if verbose_name is not provided.
563-
# Translations will happen later on LogEntry access.
564-
changed_field_labels = _get_changed_field_labels_from_form(form, changed_data)
565-
566-
change_message = []
567-
if add:
568-
change_message.append({"added": {}})
569-
elif form.changed_data:
562+
elif changed_data := form.changed_data:
563+
with translation_override(None):
564+
# Deactivate translations while fetching verbose_name for form
565+
# field labels and using `field_name`, if verbose_name is not
566+
# provided. Translations will happen later on LogEntry access.
567+
changed_field_labels = _get_changed_field_labels_from_form(
568+
form, changed_data
569+
)
570570
change_message.append({"changed": {"fields": changed_field_labels}})
571571
if formsets:
572572
with translation_override(None):

0 commit comments

Comments
 (0)