#24668: Update docs example code for ModelAdmin.save_formset()
--------------------------------------+--------------------
     Reporter:  CarstenF              |      Owner:  nobody
         Type:  Cleanup/optimization  |     Status:  new
    Component:  Documentation         |    Version:  1.8
     Severity:  Normal                |   Keywords:
 Triage Stage:  Unreviewed            |  Has patch:  0
Easy pickings:  0                     |      UI/UX:  0
--------------------------------------+--------------------
 Currently, the example code for
 
[https://docs.djangoproject.com/en/1.8/ref/contrib/admin/#django.contrib.admin.ModelAdmin.save_formset
 ModelAdmin.save_formset()] is:

 {{{
 #!python
 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
 [https://docs.djangoproject.com/en/1.8/releases/1.7/#miscellaneous 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:
 {{{
 #!diff
 diff --git a/docs/ref/contrib/admin/index.txt
 b/docs/ref/contrib/admin/index.txt
 index 416a70e..d6a5312 100644
 --- a/docs/ref/contrib/admin/index.txt
 +++ b/docs/ref/contrib/admin/index.txt
 @@ -1259,6 +1259,8 @@ templates used by the :class:`ModelAdmin` views:
          class ArticleAdmin(admin.ModelAdmin):
              def save_formset(self, request, form, formset, change):
                  instances = formset.save(commit=False)
 +                for obj in formset.deleted_objects:
 +                    obj.delete()
                  for instance in instances:
                      instance.user = request.user
                      instance.save()
 }}}

--
Ticket URL: <https://code.djangoproject.com/ticket/24668>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/051.149c96450c2aba6439cac58a21677076%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to