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

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

问题描述

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.

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

解决方案

Thanks @beholderrk, @gediminas and @jk-laiho! I packaged this into a reusable app.

Install:

pip install django-admin-list-filter-dropdown

Enable in settings.py:

INSTALLED_APPS = (
    ...
    'django_admin_listfilter_dropdown',
    ...
)

Use in 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),
    )

Here's what it looks like:

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

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