如何更改Django管理筛选器以使用下拉列表而不是列表? [英] How to change the Django admin filter to use a dropdown instead of list?

查看:127
本文介绍了如何更改Django管理筛选器以使用下拉列表而不是列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果对于要过滤的字段,您有超过〜10个值,则过滤侧边栏开始变得丑陋和难以使用。

If, for a field that you want to filter by, you have more than ~10 values, the filtering sidebar starts to be ugly and harder to use.

我正在寻找一个解决方案来替换< li> 与下拉列表选择(组合框)或类似的解决相同的问题。

I'm looking for a solution to replace the <li> with a dropdown selection (combobox) or something similar that will solve the same problem.

推荐答案

我无法评论答案,所以我将在这里添加beholderrk的答案。

I cannot comment answers so I'll add to beholderrk's answer here.


  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'


  • 现在可以使用在您的管理类中过滤:

  • now you can use this filter in your admin class:

    class SomeAdmin(admin.ModelAdmin):
        # ...
        list_filter = (('country', DropdownFilter),)
    


  • 非常棒!

    这篇关于如何更改Django管理筛选器以使用下拉列表而不是列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆