Django contrib管理员视图限制自定义

时间:2012-10-04 03:56:39

标签: python django django-admin

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

我自己在解决问题方面没有取得任何进展。我一直在搜索contrib.admin和contrib.users.admin而无法找到我正在寻找的东西。

由于

3 个答案:

答案 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)解决方案,基于DanHanswer

  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 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>
    
  6. (我在div之前的结束{% block object-tools %}{% endblock %}之前添加了它)