Show object history as a popup in Django Admin

If you wish to create a button that opens an object’s history inside a popup without the main bar and the breadcrumb, you have to override the history_view function of your ModelAdmin like this:

class MyModelAdmin(models.ModelAdmin):
...
def history_view(self, request, object_id, extra_context={}):
extra_context["is_popup"] = "_popup" in request.REQUEST
return super(ValorAdmin, self).history_view(request, object_id, extra_context)

Then, inside your templates, whenever you want to call the object’s history, you’ll have to give the link a special Javascript functionality:

<a onclick="return showAddAnotherPopup(this);" href="/app/model/id/history/">

This override guarantees two things:

  1. Whenever you click your model’s default History link inside the change view, you’ll still go to the history page the usual way (no popup, and you’ll still see the menu and the breadcrumb).
  2. Whenever you create a link using the showAddAnotherPopup function within the onclick attribute, you’ll be able to see the history as a popup, without the menu or the breadcrumb.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: