如何更改Django管理过滤器以使用下拉列表而不是列表?

sor*_*rin 36 django django-admin

如果对于要过滤的字段,您有超过10个值,则过滤侧边栏开始变得难看且难以使用.

我正在寻找一种解决方案来替换<li>下拉选择(组合框)或类似的东西来解决同样的问题.

mrt*_*rts 33

谢谢@beholderrk,@ gedas和@ jk-laiho!我把它打包成一个可重复使用的应用程序.

安装:

pip install django-admin-list-filter-dropdown
Run Code Online (Sandbox Code Playgroud)

启用settings.py:

INSTALLED_APPS = (
    ...
    'django_admin_listfilter_dropdown',
    ...
)
Run Code Online (Sandbox Code Playgroud)

用于admin.py:

from django_admin_listfilter_dropdown.filters import (
    DropdownFilter, ChoiceDropdownFilter, RelatedDropdownFilter
)

class EntityAdmin(admin.ModelAdmin):
    ...
    list_filter = (
        # for ordinary fields
        ('a_charfield', DropdownFilter),
        # for choice fields
        ('a_choicefield', ChoiceDropdownFilter),
        # for related fields
        ('a_foreignkey_field', RelatedDropdownFilter),
    )
Run Code Online (Sandbox Code Playgroud)

这是它的样子:

下拉列表过滤器的屏幕截图


Ged*_*nas 32

我不能评论答案所以我会在这里添加beholderrk的答案.

  1. 创建一个名为dropdown_filter.html或类似的新模板
  2. 将filter.html的代码从feincms复制到 dropdown_filter.html
  3. 在以下位置创建新的过滤器类filters.py:

    from django.contrib.admin.filters import AllValuesFieldListFilter
    
    class DropdownFilter(AllValuesFieldListFilter):
        template = 'admin/dropdown_filter.html'
    
    Run Code Online (Sandbox Code Playgroud)
  4. 现在您可以在管理类中使用此过滤器:

    class SomeAdmin(admin.ModelAdmin):
        # ...
        list_filter = (('country', DropdownFilter),)
    
    Run Code Online (Sandbox Code Playgroud)

效果很好!

  • 优秀.当基于外键进行过滤时,我不得不从`RelatedFieldListFilter`继承,但是相同的模板有效. (5认同)

beh*_*rrk 29

使用来自feincms的filter.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>
Run Code Online (Sandbox Code Playgroud)


Afr*_*ave 3

您可以将管理模板从 django 安装复制到项目中的 templates/admin 文件夹中。

然后,您需要在要在其中显示输出的表单或模板中执行以下两件事中的任意一项:

  1. 如果您正在使用表单,因为您希望将列表选项发布回数据库,您可以在 model.py 中的您有选择的字段中输入如下内容:

    choice = forms.IntegerField(widget=forms.Select(choices=CHOICES))
    
    Run Code Online (Sandbox Code Playgroud)
  2. 如果只是在页面上显示,那么您将在模板标签上输出如下内容:

    <select>
      {% for choices in object.details.all %}
        <option> {{ object.choice }} </option>
      {% endfor %}
    </select>
    
    Run Code Online (Sandbox Code Playgroud)