在管理站点之外重用 Django 变更列表
Django 更改列表表真的很酷 - 可搜索、可过滤、多选操作等。
我正在为应用程序构建自定义后端,并且我不断意识到:这正是我所需要的,我应该重新使用它。
有没有人有过在 Django 管理应用程序之外使用更改列表的经验?
我目前得到的是这样的:
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)
我想知道是否有人有这方面的经验或可以提出替代方案。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
ChangeList 作为一个类非常酷且功能齐全。但是,在 AdminSite 整体环境之外很难使用。
ChangeList 类需要 12 个必需的
__init__()
参数。仅这个数字就应该让您远离,而且当您意识到这些数字都来自管理员changelist_view()
时,就会加倍如此。虽然这些参数自 Django 1.1 以来一直保持不变,但它们确实从 1.0 开始发生了变化,而且它是一个 Django 内部对象,我不会依赖它的接口是否稳定。使用 ChangeList 的最佳方法 - 或者特别是获得 changelist 好处(这就是您所追求的) - 是使用
changelist_view()
方法。使用它当然需要使用/子类化 AdminSite。 这是值得做的,或者至少值得尝试。看起来您已经这样做了。该方法采用
request
参数,并且类似于指向的 URL 路由中的/(?P%s)/(?P%s)/
它。深入研究代码:
ChangeList
存在于django.contrib.admin.views.main
中changelist_view()
是django 上的一个方法。 contrib.admin.options.ModelAdmin
更新: 在 Django 1.4 中,
ChangeList
和changelist_view()
通过添加一和二进行更改分别新的参数。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 Adminchangelist_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 indjango.contrib.admin.views.main
changelist_view()
is a method ondjango.contrib.admin.options.ModelAdmin
UPDATE: In Django 1.4, both
ChangeList
andchangelist_view()
changed by adding one and two new parameters respectively.