本文介绍了在管理站点之外重用Django Changelist的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Django更改列表非常酷 - 可搜索,可过滤,多选择操作等。



我正在构建一个应用程序的自定义后端,我不断实现:这正是我需要的,我应该重新使用它。



有没有人有任何经验使用Django的管理应用程序之外的更改列表?



我目前所达到的是这样的:

  from profile.admin导入ProfileAdmin 
从django.contrib.admin.sites导入AdminSite
从profile.models导入配置文件
profile_admin = ProfileAdmin(配置文件,AdminSite())
返回profile_admin.changelist_view(请求)

我想知道有没有人有这方面的经验或者可以提出一个替代方案。 / p>

解决方案

ChangeList作为一个类是非常酷和功能完整。但是,在AdminSite巨集的上下文中难以使用。



ChangeList类需要12个必需的 __ init __( )参数。这个数字本身应该引导你走开,双重,所以当你意识到这些都来自管理员 changelist_view()。虽然Django 1.1中的这些参数保持不变,但它们从1.0变为了Django内部对象,我不会依赖其界面稳定。



使用ChangeList的最佳方式 - 或者具体来获取更改列表的优点(这是你之后) - 是使用 changelist_view()方法。使用它当然需要使用/子类化AdminSite。 这个方法需要的请求参数,并且在指向的URL路由中喜欢 /(?P< app_label>%s)/(?P< model_name>%s)/



挖掘代码:




  • ChangeList 住在 django.contrib.admin.views.main

  • changelist_view() django.contrib.admin.options.ModelAdmin

$ b的方法
$ b

更新:在Django 1.4中, ChangeList changelist_view()


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.

解决方案

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.

这篇关于在管理站点之外重用Django Changelist的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-09 11:43