diff options
| author | Daniel Fairhead <daniel@dev.ngo> | 2019-11-04 11:38:31 +0000 |
|---|---|---|
| committer | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2019-11-06 13:41:36 +0100 |
| commit | 7a13f06f3615d05e39e2b495a80e3cb1fc08e30f (patch) | |
| tree | bd3811f86b5af438c977d159b137ccb3b7ed8849 | |
| parent | 8b14545d3610fabb41b386b61985a40afc4f9520 (diff) | |
[2.2.x] 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.
Backport of e651b3095c950627b1eed1527f2bb011ddad03de from master
| -rw-r--r-- | docs/ref/contrib/admin/actions.txt | 8 |
1 files changed, 5 insertions, 3 deletions
diff --git a/docs/ref/contrib/admin/actions.txt b/docs/ref/contrib/admin/actions.txt index 91b9ff14dc..edf196360f 100644 --- a/docs/ref/contrib/admin/actions.txt +++ b/docs/ref/contrib/admin/actions.txt @@ -238,14 +238,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 that simply redirects to your custom export view:: - from django.contrib import admin from django.contrib.contenttypes.models import ContentType from django.http import HttpResponseRedirect 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) - 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 the simple part; all the complex logic would belong in your export view. This would need to deal with objects of any type, |
