#27998 closed Bug (fixed)
LogEntry messages do not list m2m fields that were changed when an object is changed via ModelAdmin
Reported by: | ljsjl | Owned by: | ljsjl |
---|---|---|---|
Component: | contrib.admin | Version: | 1.10 |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Accepted | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
What I expect to happen: m2m fields that are changed via a ModelAdmin in an update are listed in the appropriate change message in that object's history.
What happens: m2m fields are not listed in the generated change message
Reproduction: Create a vanilla Django install. Create a group. Assign a permission to that group. View the group's history, the message will say "No fields changed."
Cause: It looks like save_related() is called before form.has_changed() or form.changed_data are evaluated. save_related() calls form.save_m2m() which updates the relationship table. At this point it looks like querysets in the ModelForm form.initial have not been evaluated, so when they are evaluated as part of constructing the change message the database has already been modified by form.save_m2m(), and so the fields are left out of form.changed_data.
Fix: Probably call form.has_changed() at some point before save_related(). Happy to put together a patch for changelist_view and changeform_view if needed and attach to this ticket.
Change History (8)
comment:1 by , 8 years ago
Triage Stage: | Unreviewed → Accepted |
---|
comment:2 by , 8 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
comment:3 by , 8 years ago
Has patch: | set |
---|
comment:5 by , 7 years ago
I found that this is a regression in Django 1.10 due to ded502024191053475bac811d365ac29dca1db61 and therefore should be fixed in the current stable branch, 1.11.x. The fix for #28543 fixes this issue in a better way (at the form level, rather than only in the admin) but I'm concerned that the approach I've proposed there, changing the return type of ManyToManyField.value_from_object()
, is too risky for a stable release. I've proposed an alternate PR that modifies model_to_dict()
instead. It restores the behavior of model_to_dict()
returning a list for ManyToManyField
from before 67d984413c9540074e4fe6aa033081a35cf192bc.
comment:8 by , 7 years ago
Huh, guess I should have followed my initial fix further down the rabbit hole to Field rather than ModelAdmin behaviour. Thanks to Wonder and Tim for a better fix.
If you're able to, submitting a pull request to GitHub is the best way to submit a patch.