Login

Redirect with change list with filters intact with admin actions

Author:
AndrewIngram
Posted:
July 10, 2009
Language:
Python
Version:
1.0
Tags:
admin admin-actions
Score:
0 (after 0 ratings)

When using the django admin as a means of moderating reviews on a site, the obvious choice was to use admin actions and do everything from a single screen. The I stumbled across was that after the actions were peformed, the app redirected to the change list without any filters. This meant that filtering on un-moderated reviews was lost as soon as a change was made.

It turns out that the solution is pretty simple, you just put a redirect to request.get_full_path() at the end of the admin action. I think this should be the default behaviour, but the fix is simple nonetheless.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
def accept_selected(self, request, queryset):
    n = queryset.count()
        
    for review in queryset:
        review.accept()
        self.message_user(request, _("Successfully accepted %(count)d %(items)s.") % {
                "count": n, "items": model_ngettext(self.opts, n)
            })
        # the next line is the one you'll be interested in
        return HttpResponseRedirect(request.get_full_path())            
    accept_selected.short_description = "Accept Selected Reviews"

More like this

  1. View all log entries in the admin by jakub 3 years, 8 months ago
  2. Action that respects the filters in changeview_list by grillermo 2 years, 4 months ago
  3. Generic CSV export admin action factory with relationship spanning fields and labels by blackrobot 2 years, 2 months ago
  4. Custom management command to list recent admin actions by pbx 5 years, 7 months ago
  5. Return to a filtered changelist on change form save by richardbolt 4 years, 7 months ago

Comments

akaihola (on August 8, 2009):

The indentation for line 11 is probably wrong.

#

Please login first before commenting.