Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reusing Django Changelist Outside of Admin Site

The Django changelist table is really cool - searchable, filterable, multi-select actions etc.

I'm building a custom backend for an app and I keep realizing: this is exactly what I need, I should re-use it.

Has anyone had any experience using the change list outside of Django's admin app?

What I've arrived at currently is something like this:

from profile.admin import ProfileAdmin
from django.contrib.admin.sites import AdminSite
from profile.models import Profile
profile_admin = ProfileAdmin(Profile, AdminSite())
return profile_admin.changelist_view(request)

I'd like to know if anyone has had experience with this or can suggest an alternative.

like image 887
Koobz Avatar asked Feb 23 '10 01:02

Koobz


1 Answers

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.

like image 189
JCotton Avatar answered Sep 19 '22 23:09

JCotton