Login

Redirect with change list with filters intact with admin actions

Author:
AndrewIngram
Posted:
July 10, 2009
Language:
Python
Version:
1.0
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. Template tag - list punctuation for a list of items by shapiromatron 9 months ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 9 months, 1 week ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 4 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 4 months ago
  5. Help text hyperlinks by sa2812 1 year, 5 months ago

Comments

akaihola (on August 8, 2009):

The indentation for line 11 is probably wrong.

#

Please login first before commenting.