Fixed #15742 -- Fixed an example of collecting selected objects in ModelAdmin.actions docs.

The queryset argument is already filtered, and request.POST doesn't
contain all selected objects when "Select All" is used.
This commit is contained in:
Daniel Fairhead 2019-11-04 11:38:31 +00:00 committed by Mariusz Felisiak
parent 74f2a58b3a
commit e651b3095c
1 changed files with 5 additions and 3 deletions

View File

@ -236,14 +236,16 @@ you'd want to let the user choose a format, and possibly a list of fields to
include in the export. The best thing to do would be to write a small action include in the export. The best thing to do would be to write a small action
that redirects to your custom export view:: that redirects to your custom export view::
from django.contrib import admin
from django.contrib.contenttypes.models import ContentType from django.contrib.contenttypes.models import ContentType
from django.http import HttpResponseRedirect from django.http import HttpResponseRedirect
def export_selected_objects(modeladmin, request, queryset): def export_selected_objects(modeladmin, request, queryset):
selected = request.POST.getlist(admin.ACTION_CHECKBOX_NAME) selected = queryset.values_list('pk', flat=True)
ct = ContentType.objects.get_for_model(queryset.model) ct = ContentType.objects.get_for_model(queryset.model)
return HttpResponseRedirect("/export/?ct=%s&ids=%s" % (ct.pk, ",".join(selected))) return HttpResponseRedirect('/export/?ct=%s&ids=%s' % (
ct.pk,
','.join(str(pk) for pk in selected),
))
As you can see, the action is rather short; all the complex logic would belong As you can see, the action is rather short; all the complex logic would belong
in your export view. This would need to deal with objects of any type, hence in your export view. This would need to deal with objects of any type, hence