1

我正在寻找一种将寻呼机限制公开为管理视图中的表单的方法,方法是添加诸如&?pager_limit=50或类似的查询参数,以便它可以与各种公开的过滤器愉快地合并。

我自己在解决问题方面并没有真正取得任何进展。我一直在搜寻 contrib.admin 和 contrib.users.admin 却找不到我要找的东西。

谢谢

4

3 回答 3

4

为了使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)

然后get_changelist在 ModelAdmin 子类中覆盖:

class CustomUserAdmin(UserAdmin):

    def get_changelist(self, request, **kwargs):
        return CustomUserChangeList
于 2012-10-12T05:25:06.183 回答
1
class ActionAdmin(admin.ModelAdmin):
    list_per_page = 25

您可以在此处指定它。

于 2012-10-11T11:08:28.360 回答
0

这是我根据DanH回答提出的(笨拙的)解决方案:

  1. 创建一个全局函数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
    
  2. 在相关的 ModelAdmin 实例中,添加对该函数的调用,get_changelist如示例中所示:

    class DeviceTypeAdmin(admin.ModelAdmin):
    
        def get_changelist(self, request, **kwargs):
            return return_changelist_for_request(request)
    
        list_display = ('devicetype', )
    
  3. urls.py中,添加一个page_size查看链接,我们稍后会添加:

    url(r'^page_size$', 'viewer.views.page_size', name='page_size'),
    
  4. 将视图添加到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'))
    
  5. 将 Django 管理员的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 %}

于 2015-07-16T13:41:36.637 回答