Skip to content

Instantly share code, notes, and snippets.

@lincolnloop
Created May 12, 2009 15:12
Show Gist options
  • Save lincolnloop/110533 to your computer and use it in GitHub Desktop.
Save lincolnloop/110533 to your computer and use it in GitHub Desktop.
class KeepQueryStringModelAdmin(admin.ModelAdmin):
"""
A ModelAdmin that "fixes" the default behavior of redirecting to the
default change list after a save.
This class remembers the change list query string that was used for the
change list and redirects to it after a save, maintaining the ordering,
filtering, & pagination.
Thanks to Vlada Macek http://djangopeople.net/tuttle/
"""
def _qstr_sesskey(self):
return 'last_admin_changelist_query_string_' + repr(self.__class__)
def changelist_view(self, request, extra_context=None):
"""
Remember the listing pager, ordering and filtering...
"""
if hasattr(request, 'session'):
request.session[self._qstr_sesskey()] =
request.META.get('QUERY_STRING')
return super(KeepQueryStringModelAdmin, self).changelist_view(
request, extra_context)
def response_change(self, request, obj):
"""
... and restore it after "Save" button is used.
"""
r = super(KeepQueryStringModelAdmin, self).response_change(
request, obj)
if hasattr(request, 'session'):
l = request.session.get(self._qstr_sesskey())
if r['Location'] == '../' and l:
r['Location'] += '?' + l
return r
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment