Login

Admin: return to change_list with filter and pagination applied

Author:
fx
Posted:
April 13, 2011
Language:
Python
Version:
1.2
Score:
4 (after 4 ratings)

By default every time you change and save an object in the admin, the change_list "jumps" to the first page, so filters you used to find the object (or the pagination-page) have to be applied again. If you have to go through a multi-object-list step-by-step this could become really annoying.

The above snippet changes this behaviour by returning to the referring URL when saving. Included in this URL are variables for the filters/pagination.

The snippet is part of your custom Model.admin in admin.py.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class DocumentAdmin(admin.ModelAdmin):

    def change_view(self, request, object_id, extra_context={}):
        """
        save the referer of the page to return to the filtered
        change_list after saving the page
        """
        
        if len(extra_context) > 0:
            """ 
            second call of the form (after saving), take the
            referer from extra_context['ref'], clear extra_context 
            and put the referer in result['Location'] 
            """                        
            ref = extra_context['ref']            
            result = super(DocumentAdmin, self).change_view(request, object_id, extra_context.clear() )
            result['Location'] = ref            
        else:
            """ 
            first call of the form: add the referer to the context  
            """                       
            extra_context['ref'] = unicode( request.META.get('HTTP_REFERER', '') ) 
            result = super(DocumentAdmin, self).change_view(request, object_id, extra_context )
        return result

admin.site.register(Document, DocumentAdmin)

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 3 months ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 3 months, 1 week ago
  3. Serializer factory with Django Rest Framework by julio 10 months, 1 week ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 10 months, 3 weeks ago
  5. Help text hyperlinks by sa2812 11 months, 3 weeks ago

Comments

arie (on April 14, 2011):

This is really useful. I have some links to the admin change_view in my frontend and i was looking for an easy redirection (back to the frontend) after an object is saved. This seems to to do it.

How would i easily provide the enhanced change_view for all my ModelAdmins. Can i easily subclass ModelAdmin, change the change_view and make my models use MyModelAdmin? Or will i run into trouble doing this ... ?

#

gamesbook (on April 14, 2011):

Ditto re arie's comment - I think subclassing, or even a mixin is the way to go ... but what are the "side effects"?

#

gamesbook (on April 14, 2011):

Follow-up: the code, as at 14 April 2011, would need to be changed as it refers to "DocumentAdmin" i.e. it is not actually generic code and so could not be used via a subclass/mixin approach...

#

doubtintom (on April 15, 2011):

This snippet works when using the built-in Django server, but fails when running with Apache & WSGI.

Using Django 1.3, Python 2.6.5

Was able to run a debugger, saw both branches of the if statement fire when I clicked on a link in the changelist, then when I changed and saved the record, the both fired again. So instead of firing once when I drilled down from the change list (save the location and filter info) and once again to retrieve the location & filter info, it does that cycle twice on each click and just brings the unfiltered changelist back.

Any ideas?

#

doubtintom (on April 16, 2011):

Oh, uh, it seems to be working with Apache now. Maybe it always did. But the save/restore of the extra context can get out of whack with certain navigation patterns I haven't nailed down to a deterministic test case yet, and then the unfiltered changelist appears upon save.

#

Please login first before commenting.