我正在寻找一种方法,通过添加查询参数(例如&?pager_limit=50
或类似的东西)将寻呼机限制作为管理视图中的表单公开,以便它可以与各种公开的过滤器愉快地合并。
我自己在解决问题方面没有取得任何进展。我一直在搜索contrib.admin和contrib.users.admin而无法找到我正在寻找的东西。
由于
答案 0 :(得分:3)
为了使list_per_page
动态,我们需要继承ChangeList
,因为默认的ChangeList
类会将list_per_page
设置一次。
例如:
class CustomUserChangeList(ChangeList):
def __init__(self, request, model, list_display, list_display_links,
list_filter, date_hierarchy, search_fields, list_select_related,
list_per_page, list_max_show_all, list_editable, model_admin):
list_per_page = get_user_list_limit()
super(CustomUserChangeList, self).__init__(request, model, list_display, list_display_links,
list_filter, date_hierarchy, search_fields, list_select_related,
list_per_page, list_max_show_all, list_editable, model_admin)
然后在ModelAdmin子类中重写get_changelist
:
class CustomUserAdmin(UserAdmin):
def get_changelist(self, request, **kwargs):
return CustomUserChangeList
答案 1 :(得分:2)
class ActionAdmin(admin.ModelAdmin):
list_per_page = 25
您可以在此处指定。
答案 2 :(得分:0)
这是我提出的一个(kludgy)解决方案,基于DanH的answer:
创建一个全局函数return_changelist_for_request
,可见admin.py
:
def return_changelist_for_request(request):
class CustomUserChangeList(django.contrib.admin.views.main.ChangeList):
def __init__(self, request, model, list_display, list_display_links,
list_filter, date_hierarchy, search_fields, list_select_related,
list_per_page, list_max_show_all, list_editable, model_admin):
try:
page_size = request.session.get('page_size', 100)
list_per_page = int(page_size)
except ValueError:
list_per_page = 100
request.session['page_size'] = list_per_page
super(CustomUserChangeList, self).__init__(request,
model, list_display, list_display_links, list_filter,
date_hierarchy, search_fields, list_select_related,
list_per_page, list_max_show_all, list_editable,
model_admin)
return CustomUserChangeList
在相关的ModelAdmin实例中,添加对此函数的调用,名为get_changelist
,如示例所示:
class DeviceTypeAdmin(admin.ModelAdmin):
def get_changelist(self, request, **kwargs):
return return_changelist_for_request(request)
list_display = ('devicetype', )
在urls.py
中,添加指向我们将在一秒内添加的page_size
视图的链接:
url(r'^page_size$', 'viewer.views.page_size', name='page_size'),
将视图添加到views.py
:
from django.http import HttpResponseRedirect
def page_size(request):
request.session['page_size'] = request.GET['page_size']
return HttpResponseRedirect(request.META.get('HTTP_REFERER'))
将Django admin的base.html
模板从site-packages复制到您的模板目录并添加类似的HTML代码(此HTML示例假定为django-admin-bootstrapped):
<form class="navbar-form navbar-left" action='/page_size'>
<div class="form-group">
<input type="text" class="form-control" placeholder="Size" name="page_size" size="3">
</div>
<button type="submit" class="btn btn-default">Set page size</button>
</form>
(我在div
之前的结束{% block object-tools %}{% endblock %}
之前添加了它)