Reusing Django Changelist Outside of Admin Site

末鹿安然 提交于 2019-11-30 05:15:25
JCotton

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.

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