#28543 closed Bug (fixed)
ModelForm.initial is affected while its bound instance's m2m field be set with new data
Reported by: | Wonder | Owned by: | nobody |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | 1.11 |
Severity: | Normal | Keywords: | form |
Cc: | Triage Stage: | Accepted | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
I found Django's admin log always doesn't include m2m-fields' change. So I looked into django source and found here: https://github.com/django/django/blob/1.11.4/django/contrib/admin/options.py#L1410.
After this line self.save_related(request, form, formsets, not add)
, form.initial
is also changed to the saved version, then affect form.changed_data
.
Then I wrote some test code to reproduce the issue:
import datetime as dt from django.contrib.auth.forms import UserChangeForm from django.contrib.auth import get_user_model from django.contrib.auth.models import Group User = get_user_model() g1=Group.objects.get_or_create(name='g1')[0] g2=Group.objects.get_or_create(name='g2')[0] user = User.objects.get_or_create(username='wonder')[0] user.groups.set([g1]) form = UserChangeForm({ 'username': 'wonder', 'date_joined': dt.datetime.now(), }, instance=user) assert form.is_valid() form.initial['groups'] # Out: <QuerySet [<Group: g1>]> user.groups.set([g2]) form.initial['groups'] # Out: <QuerySet [<Group: g2>]>
Note the two comments in the code.
Change History (6)
comment:1 by , 7 years ago
comment:2 by , 7 years ago
The issue of ManyToManyFields not appearing in the admin's change history is fixed in master by #27998. Is there another issue present? I'm unsure since your steps to reproduce don't involve the admin.
Yes, It may affect not only m2m-fields change in admin.
comment:3 by , 7 years ago
Component: | Uncategorized → Database layer (models, ORM) |
---|---|
Has patch: | set |
Triage Stage: | Unreviewed → Accepted |
Type: | Uncategorized → Bug |
comment:4 by , 7 years ago
I found that #27998 is a regression in Django 1.10 due to ded502024191053475bac811d365ac29dca1db61. The proposed patch here fixes that issue in a better way (at the form level, rather than only in the admin) but, with respect to the decision of how to fix that on stable/1.11.x, changing the return type of ManyToManyField.value_from_object()
may be too risky for a stable release. Perhaps a less invasive fix for 1.11.x would restore the queryset to list conversion for ManyToManyFields in model_to_dict()
.
The issue of ManyToManyFields not appearing in the admin's change history is fixed in master by #27998. Is there another issue present? I'm unsure since your steps to reproduce don't involve the admin.