4

As a new feature in Django 1.8, we have InlineModelAdmin.show_change_link, which, in the words of the documentation,

Specifies whether or not inline objects that can be changed in the admin have a link to the change form.

This is great, and I've been looking for this feature to be added for a long time.

Just one problem.

Say I'm on the admin change form for an instance of some model that has inlines (e.g., consider the Poll model, with Choice inlines, from the Django tutorial). I use the new "Change" link to go to the full change form for one of the Choices. I make some edits and click "Save".

I would expect to be taken back where I came from — that is, to the change form for the Poll instance. Instead, I am taken to the list of all Choice instances.

How can I have Django remember that if I came from a list of inlines, I should go back there on "Save"? (But if I ever do edit the Choice directly from the list of all Choices, I should go back there.)

thecommexokid
  • 303
  • 2
  • 13

1 Answers1

1

You can make Django go back to the Poll instance by overriding the response_change method on the full Choice admin:

from django.core.urlresolvers import reverse
from django.http import HttpResponseRedirect

class ChoiceAdmin(admin.ModelAdmin):
    '''
    To be called from the Poll choice inline. It will send control back 
    to the Poll change form, not the Choice change list.
    '''
    fields = [...]

    def response_change(self, request, choice):
        if not '_continue' in request.POST:
            return HttpResponseRedirect(reverse("admin:appname_poll_change", args=(choice.poll.id,)))
        else:
            return super(ChoiceAdmin, self).response_change(request, choice)

To address the second part of your question: I think you'll have to register a second, unmodified, admin on the model. You can do that using a proxy model. See Multiple ModelAdmins/views for same model in Django admin

Community
  • 1
  • 1