#24668 closed Cleanup/optimization (fixed)
Update docs example code for ModelAdmin.save_formset()
Reported by: | Carsten Fuchs | Owned by: | nobody |
---|---|---|---|
Component: | Documentation | Version: | 1.8 |
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
Currently, the example code for ModelAdmin.save_formset() is:
class ArticleAdmin(admin.ModelAdmin): def save_formset(self, request, form, formset, change): instances = formset.save(commit=False) for instance in instances: instance.user = request.user instance.save() formset.save_m2m()
In the text, there is a link to https://docs.djangoproject.com/en/1.8/topics/forms/modelforms/#saving-objects-in-the-formset, but not to https://docs.djangoproject.com/en/1.8/topics/forms/formsets/#can-delete, where it is mentioned that with Django 1.7, formset.save(commit=False)
no longer deletes objects automatically.
(This is also mentioned in the Django 1.7 Release Notes, but for me at least, this was not enough to make the required mental leap...)
Thus, I suggest to augment the above example code as follows:
-
docs/ref/contrib/admin/index.txt
diff --git a/docs/ref/contrib/admin/index.txt b/docs/ref/contrib/admin/index.txt index 416a70e..d6a5312 100644
a b templates used by the :class:`ModelAdmin` views: 1259 1259 class ArticleAdmin(admin.ModelAdmin): 1260 1260 def save_formset(self, request, form, formset, change): 1261 1261 instances = formset.save(commit=False) 1262 for obj in formset.deleted_objects: 1263 obj.delete() 1262 1264 for instance in instances: 1263 1265 instance.user = request.user 1264 1266 instance.save()
That makes sense.
Could you submit your proposed change as a pull request?
Thanks.