Opened 6 weeks ago
Closed 12 days ago
#36801 closed Cleanup/optimization (fixed)
`construct_change_message`call `form.changed_data` after check if is add
| Reported by: | Rodolfo Becerra | Owned by: | Rodolfo Becerra |
|---|---|---|---|
| Component: | contrib.admin | Version: | |
| Severity: | Normal | Keywords: | construct_change_message |
| Cc: | Rodolfo Becerra | Triage Stage: | Ready for checkin |
| Has patch: | yes | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | yes | UI/UX: | no |
Description (last modified by )
This is the currently code of the method construct_change_message of django.contrib.admin.utils
def construct_change_message(form, formsets, add): """ Construct a JSON structure describing changes from a changed object. Translations are deactivated so that strings are stored untranslated. Translation happens later on LogEntry access. """ # 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: change_message.append({"changed": {"fields": changed_field_labels}})
If you notice that the form.changed_data property is being called unnecessarily before checking whether it is an add behavior. I propose call the changed_data = form.changed_data after check if is add.
If I am accepted, I would like to create the PR myself.
Change History (5)
comment:1 by , 6 weeks ago
| Description: | modified (diff) |
|---|
comment:2 by , 6 weeks ago
| Resolution: | → needsinfo |
|---|---|
| Status: | assigned → closed |
comment:3 by , 3 weeks ago
| Has patch: | set |
|---|---|
| Resolution: | needsinfo |
| Status: | closed → new |
Rodolfo created this ticket at my encouragement, after he showed me a diff of the change he meant. I think the description didn't quite do it justice, so I've created a PR to show what he meant. I think it's a pretty clear optimization.
Hi Rodolfo. So
changed_datais a cached property to avoid having to reason about this in various places. Given that, I'm inclined to close asneedsinfopending a benchmark showing this is worth devoting a review cycle to. This is generally how we handle all optimization proposals.