Login

Return to a filtered changelist on change form save

Author:
richardbolt
Posted:
August 6, 2010
Language:
Python
Version:
1.2
Score:
2 (after 2 ratings)

You've filtered your changelist in your admin site and you want to edit a few entries here; you click on an object, edit it and press save, and you end up back at the default unfiltered changelist view. This ModelAdmin override is so that if you press "save" (not "save and add another", or "save and continue editing") you end up back at your filtered changelist.

There are other ways out there and other snippets to do similar; however I hadn't seen one to only redirect if you pressed save so this is what I came up with. Hopefully it's useful.

 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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import urlparse

from django.contrib import admin
from django.core.urlresolvers import reverse
from django.http import HttpResponseRedirect
from django.utils.translation import ugettext_lazy, ugettext as _
from django.utils.encoding import force_unicode

class MyModelAdmin(admin.ModelAdmin):
    
    def response_change(self, request, obj):
        """
        Determines the HttpResponse for the change_view stage.
        
        We are returning the user to a filtered changelist page
        from the change screen when the user presses save.
        This makes for more intutive editing of filtered objects
        since upon saving you will be redirected back to the filtered
        changelist page.
        
        Requires that in your admin/change_form.html you add a hidden
        form input named "_go_to_on_save" that you can populate with
        the value from request.META.HTTP_REFERER as below:
        {% block after_related_objects %}{% if request.META.HTTP_REFERER %}<input type="hidden" name="_go_to_on_save" value="{{ request.META.HTTP_REFERER }}" />{% endif %}{% endblock %}
        """
        opts = obj._meta

        msg = _('The %(name)s "%(obj)s" was changed successfully.') % {'name': force_unicode(opts.verbose_name), 'obj': force_unicode(obj)}
        
        if request.POST.has_key("_go_to_on_save") and urlparse.urlparse(request.POST['_go_to_on_save']).query\
                and request.get_host() in request.POST['_go_to_on_save']\
                and not (request.POST.has_key("_continue") or request.POST.has_key("_saveasnew") or\
                request.POST.has_key("_addanother")):
            self.message_user(request, msg)
            change_url = reverse('admin:%s_%s_changelist' % (self.model._meta.app_label, self.model._meta.module_name))
            potential_go_to_url = urlparse.urlparse(request.POST['_go_to_on_save'])
            if change_url == potential_go_to_url.path:
                query = urlparse.urlparse(request.POST['_go_to_on_save']).query
                return HttpResponseRedirect(change_url+'?'+query)
        
        return super(MyModelAdmin, self).response_change(request, obj)

More like this

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

Comments

Please login first before commenting.