1👍
Save your query string in the session. Add a ‘Save and filter’ button to your change view. Add a responce_change to your Admin.
To save your query string in the session. Make a new file ‘BasicContextProcessor.py’ in the root of your project. It contains:
def basics(request):
query_string = request.META['QUERY_STRING']
if query_string:
request.session['query_string'] = query_string
return
You probably want to extend the example above with logic to store query_string only when appropriate. Also if you have multiple change views with a ‘save and filter’ button, you should save the query_string for each model separate. Otherwise filters for list_view-x will be applied to list_view-y.
To process BasicContextProcessor.basics every request add it to TEMPLATE_CONTEXT_PROCESSORS in Settings.py:
TEMPLATE_CONTEXT_PROCESSORS = (
...
"BasicContextProcessor.basics"
)
Add the ‘Save and filter’ button to your change view. To extend change_form.html, add a file ‘/app_name/templates/admin/book/change_form.html’. Add a button with jQuery:
{% extends "admin/change_form.html" %}
{% load i18n %}
{% block content %}
{{ block.super }}
<script type="text/javascript">//<![CDATA[
(function($){
$('<input type="submit" value="Save and filter" name="_viewfilterlist" />').prependTo('div.submit-row');
})(django.jQuery);
//]]></script>
{% endblock %}
You can also replace the Save button to make this the default behaviour. But I like to give the user both options.
Change the response. In admin.py BookAdmin add:
def response_change(self, request, obj):
if request.POST.has_key("_viewfilterlist"):
msg = (_('The %(name)s "%(obj)s" was changed successfully.') %
{'name': force_unicode(obj._meta.verbose_name),
'obj': force_unicode(obj)})
query_string = request.session['query_string']
if query_string:
self.message_user(request, msg)
return HttpResponseRedirect("/admin/app_name/book/?%s" % query_string)
return super(Book, self).response_change(request, obj)
I didn’t test these example snippets. But used this technique for other response changes. Wich worked. This should get you started. But if you find errors, I like to know. And I’ll update the answer.
Happy coding!
1👍