Admin Save and view next button

 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
"""admin.py"""
from django.contrib import admin
from django.http import HttpResponseRedirect
from django.utils.encoding import force_unicode
from django.utils.translation import ugettext_lazy as _
from myapp.models import MyModel


class MyAdmin(admin.ModelAdmin):
    def response_change(self, request, obj):
        """
        Determines the HttpResponse for the change_view stage.
        """
        if request.POST.has_key("_viewnext"):
            msg = (_('The %(name)s "%(obj)s" was changed successfully.') %
                   {'name': force_unicode(obj._meta.verbose_name),
                    'obj': force_unicode(obj)})
            next = obj.__class__.objects.filter(id__gt=obj.id).order_by('id')[:1]
            if next:
                self.message_user(request, msg)
                return HttpResponseRedirect("../%s/" % next[0].pk)
        return super(MyAdmin, self).response_change(request, obj)


admin.site.register(MyModel, MyAdmin)

More like this

  1. Admin Save and view next button html by ungenio41 3 years, 11 months ago
  2. URLField admin widget with View Link button by ungenio41 3 years, 11 months ago
  3. Add buttons in admin forms by mamat 5 years, 7 months ago
  4. Button Admin by delfick 4 years, 3 months ago
  5. Admin page without inlines dinamically by davmuz 1 year, 10 months ago

Comments

gamesbook (on December 11, 2012):

The problem is that this does not test where the "next" item is in terms of the existing ordering.

#

(Forgotten your password?)