Opened 10 years ago

Closed 10 years ago

Last modified 10 years ago

#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:  
    12591259        class ArticleAdmin(admin.ModelAdmin):
    12601260            def save_formset(self, request, form, formset, change):
    12611261                instances = formset.save(commit=False)
     1262                for obj in formset.deleted_objects:
     1263                    obj.delete()
    12621264                for instance in instances:
    12631265                    instance.user = request.user
    12641266                    instance.save()

Change History (4)

comment:1 by Baptiste Mispelon, 10 years ago

Has patch: set
Triage Stage: UnreviewedAccepted

That makes sense.

Could you submit your proposed change as a pull request?

Thanks.

comment:2 by Tim Graham <timograham@…>, 10 years ago

Resolution: fixed
Status: newclosed

In e15c55e2:

Fixed #24668 -- Amended ModelAdmin.save_formset() example for #10284.

Thanks Carsten Fuchs.

comment:3 by Tim Graham <timograham@…>, 10 years ago

In 32871fc:

[1.7.x] Fixed #24668 -- Amended ModelAdmin.save_formset() example for #10284.

Thanks Carsten Fuchs.

Backport of e15c55e2bf5eb2f1073d18b4ee6fdce2369820f2 from master

comment:4 by Tim Graham <timograham@…>, 10 years ago

In e02616b:

[1.8.x] Fixed #24668 -- Amended ModelAdmin.save_formset() example for #10284.

Thanks Carsten Fuchs.

Backport of e15c55e2bf5eb2f1073d18b4ee6fdce2369820f2 from master

Note: See TracTickets for help on using tickets.
Back to Top