[Fixed]-Reusing Django Changelist Outside of Admin Site

9👍

ChangeList as a class is really cool and feature-full. However, it’s hard to use outside the context of the AdminSite monolith.

The ChangeList class takes 12 required __init__() parameters. That number alone should steer you away and doubly so when you realize those are all sourced from the Admin changelist_view(). While those parameters have remained the same since Django 1.1, they did change from 1.0 and it’s so much a Django internal object, I wouldn’t rely on its interface being stable.

The best way to use ChangeList — or specifically to get the changelist benefits (which is what you are after) — is to use the changelist_view() method. Using that of course requires using/subclassing AdminSite. This is worth doing, or at least trying out. Looks like you already are.

That method takes the request parameter and likes /(?P<app_label>%s)/(?P<model_name>%s)/ in the URL route that points to it.

Digging into the code:

  • ChangeList lives in django.contrib.admin.views.main
  • changelist_view() is a method on django.contrib.admin.options.ModelAdmin

UPDATE: In Django 1.4, both ChangeList and changelist_view() changed by adding one and two new parameters respectively.

Leave a comment