Admin: return to change_list with filter and pagination applied

 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. Return to change_list with filter after change by graveyboat 2 years, 8 months ago
  2. keeping filter states after edits by chronosllc 2 years, 8 months ago
  3. Keeping filter states after edits (Django 1.4) by rhfung 1 year, 5 months ago
  4. Additional Change List Columns by sansmojo 6 years, 9 months ago
  5. Improved Button Admin by kunitoki 2 years, 2 months 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.

#

(Forgotten your password?)