Posted By

magicrebirth on 01/26/12


Tagged

admin


Versions (?)

Override the change_view in the admin


 / Published in: Django
 

URL: https://code.djangoproject.com/browser/django/trunk/django/contrib/admin/options.py#L445

Adding another button with name "addnextid" would trigger our custom action, that redirects the user to the changeform screen for the next available item (by ID).

Then you can override admin/submitline.html. Copy the version in contrib.admin.templates into your project. Mine is myproject/templates/admin/submitline.html, but you could use /myproject/myapp/templates/admin/submit_line.html.

Next, edit the copy and add the code for showing the 'Save and edit next item (by ID)' link, which is caught via the "_addnextid" name.

p.s. The submitline.html template is called in changeform.html via the {% submit_row %} tag.

  1. # in your models.py, or admin.py
  2.  
  3. from django.http import HttpResponseRedirect
  4. from django.utils.encoding import force_unicode
  5. from django.utils.translation import ugettext_lazy as _
  6. from django.core.urlresolvers import reverse
  7.  
  8. class Admin(ModelAdmin):
  9.  
  10.  
  11. # ps: this will catch the change action only; for add actions override the response_add method in a similar way
  12. def response_change(self, request, obj):
  13. """ custom method that cacthes a new 'save and edit next' action
  14. Remember that the type of 'obj' is the current model instance, so we can use it dynamically!
  15. """
  16. opts = obj._meta
  17. verbose_name = opts.verbose_name
  18. module_name = opts.module_name
  19. pk_value = obj._get_pk_val()
  20.  
  21. if "_addnextid" in request.POST:
  22. msg = _("""The %(name)s "%(obj)s" was added successfully. Now you're editing the following %(name)s, according to its ID number.""") % {'name': force_unicode(verbose_name), 'obj': obj}
  23. self.message_user(request, msg)
  24.  
  25. try:
  26. next_obj = [x.id for x in obj.__class__.objects.filter(id__gt=pk_value).order_by('id')][0]
  27. except:
  28. print "ERROR"
  29. next_obj = pk_value
  30. return HttpResponseRedirect(reverse('admin:%s_%s_change' %
  31. (opts.app_label, module_name),
  32. args=(next_obj,),
  33. current_app=self.admin_site.name))
  34. else:
  35. return super(obj.Admin, self).response_change(request, obj)
  36.  
  37.  
  38.  
  39. # OPTION 1 : then override submit_line.html => it'll make the new button appear on all change forms
  40.  
  41.  
  42. {% load i18n %}
  43. <div class="submit-row" {% if is_popup %}style="overflow: auto;"{% endif %}>
  44. {% if show_save %}<input type="submit" value="{% trans 'Save' %}" class="default" name="_save" {{ onclick_attrib }}/>{% endif %}
  45. {% if show_delete_link %}<p class="deletelink-box"><a href="delete/" class="deletelink">{% trans "Delete" %}</a></p>{% endif %}
  46. {% if show_save_as_new %}<input type="submit" value="{% trans 'Save as new' %}" name="_saveasnew" {{ onclick_attrib }}/>{%endif%}
  47.  
  48. {# ADD ON: EDIT NEXT ID ITEM LINK: we don't show it if a new item is being added by using the 'delete' flag #}
  49.  
  50. {% if show_delete_link %}<input type="submit" value="{% trans 'Save and edit next item (by ID)' %}" name="_addnextid" {{ onclick_attrib }} />{% endif %}
  51.  
  52. {% if show_save_and_add_another %}<input type="submit" value="{% trans 'Save and add another' %}" name="_addanother" {{ onclick_attrib }} />{% endif %}
  53. {% if show_save_and_continue %}<input type="submit" value="{% trans 'Save and continue editing' %}" name="_continue" {{ onclick_attrib }}/>{% endif %}
  54. </div>
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62. # OPTION 2 : add the new button via js ==> you can more easily decide which model template the button should appear on
  63. # http://stackoverflow.com/questions/3874231/adding-an-extra-button-to-one-object-in-django-admin
  64.  
  65.  
  66. # static/js/admin_addon.js
  67.  
  68. function update_document() {
  69. // for the Source template:
  70. $('input[name="_addanother"]').before('<input type="submit" name="_addnextid" value="Save and edit next item (by ID)"/>');
  71.  
  72.  
  73. }
  74.  
  75. // give time to jquery to load..
  76. setTimeout("update_document();", 1000);
  77.  
  78.  
  79.  
  80. # models.py
  81.  
  82. class MyModelAdmin(admin.ModelAdmin):
  83. class Media:
  84. js = ("js/admin_addon.js",)

Report this snippet  

You need to login to post a comment.