Reusing Django Changelist Outside of Admin Site

时间秒杀一切 提交于 2019-11-29 02:43:43

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/2315500/reusing-django-changelist-outside-of-admin-site

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!