【问题标题】:Django: Admin list_filter getting too longDjango:管理员 list_filter 变得太长
【发布时间】:2019-03-12 22:47:33
【问题描述】:

我的模型的 change_list 视图如下所示:

右侧的list_filter 太长且无法使用。

有没有办法获得更有用的方法来过滤 Django 的 change_list 视图?

【问题讨论】:

标签: django django-admin


【解决方案1】:

您可以尝试使用 dal_admin_filters 它的 Django 自动完成灯光过滤器用于 django 管理员

https://pypi.org/project/dal_admin_filters/

看起来可能就是您要找的东西

【讨论】:

    【解决方案2】:

    虽然dal_admin_filters 似乎可以工作,但它已经有一段时间没有更新(2017 年),并且早于 Django 2.0 中自动完成字段的引入。

    如果我是你,我会根据 Django 2.0 和 dal_admin_filters 实现中公开的新自动完成端点实现我自己的 SimpleListFilter subclass 试试运气。

    【讨论】:

      【解决方案3】:

      你可以试试这样的。

      注意:Gedas here的原帖

      1. 创建一个名为custom_filter.html 的新模板
      2. 将以下代码复制到custom_filter.html。我包括两个不同的版本 - 一个带有单选,另一个带有多选。

      custom_filter_single.html

      {% load i18n %}
      <script type="text/javascript">
      var go_from_select = function(opt) 
      { window.location = window.location.pathname + opt };
      </script>
        <h3>{{ title }}</h3>
        <ul class="admin-filter-{{ title|cut:' ' }}">
        {% if choices|slice:"4:" %}
        <li>
        <select style="width: 95%;"
          onchange="go_from_select(this.options[this.selectedIndex].value)">
          {% for choice in choices %}
             <option{% if choice.selected %} selected="selected"{% endif %}
               value="{{ choice.query_string|iriencode }}">{{ choice.display }}
             </option>
          {% endfor %}
        </select>
        </li>
        {% else %}
        {% for choice in choices %}
          <li{% if choice.selected %} class="selected"{% endif %}>
            <a href="{{ choice.query_string|iriencode }}">{{ choice.display }}</a> 
          </li>
        {% endfor %}
      
        {% endif %}
        </ul>
      

      custom_filter_multiple.html 使用select2 lib,下面可以优化

      {% load i18n %}
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"> 
      </script>
      <link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.6- 
      rc.0/css/select2.min.css" rel="stylesheet" />
      <script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.6- 
      rc.0/js/select2.min.js"></script>
      
      <script>
         $(document).ready(function() {
          $("#personnel").select2();
          $("#personnel").change(function() {
          var selected_vals = $('#personnel').val();
          var selections = selected_vals.join().replace(/\?/g, '').replace(/\,/g, 
          '&');
          window.location = window.location.pathname + "?" +selections;
          });
      
        });
      </script>
      <h3>{{ title }}</h3>
      <ul class="admin-filter-{{ title|cut:' ' }}">
      {% if choices|slice:"4:" %}
        <li>
         <select style="width: 95%;" class="js-example-basic-multiple" 
          multiple="multiple" id="personnel">
         {% for choice in choices %}
         <option
            value="{{ choice.query_string|iriencode }}">{{ choice.display }}
         </option>
         {% endfor %}
         </select>
        </li>
      
        {% else %}
        {% for choice in choices %}
             <li{% if choice.selected %} class="selected"{% endif %}>
              <a href="{{ choice.query_string|iriencode }}">{{ choice.display }}</a> 
             </li>
        {% endfor %}
      
        {% endif %}
        </ul>
      
      1. 在 filters.py 中创建一个名为 CustomFilter 的新过滤器类

        从 django.contrib.admin.filters 导入 AllValuesFieldListFilter

        类 CustomFilter(AllValuesFieldListFilter): template = 'admin/custom_filter_single.html' #使用合适的模板

      2. 现在像这样在admin.py 中使用上面的过滤器类

        class SomeAdmin(admin.ModelAdmin):
            list_filter = (('personnel_type', CustomDropDownFilter),)
        #TIP: for custom filter labels, you can add verbose_name in 
        #models.py like this. 
        personnel_type = models.CharField(max_length=32, blank=True, 
                          verbose_name="By Personnel Type")
        

      一些截图。

      【讨论】:

        猜你喜欢
        • 2012-04-06
        • 2013-05-09
        • 2011-10-08
        • 1970-01-01
        • 1970-01-01
        • 2018-02-19
        • 1970-01-01
        • 1970-01-01
        • 2010-10-12
        相关资源
        最近更新 更多