如何更改 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.
I'm looking for a solution to replace the <li>
with a dropdown selection (combobox) or something similar that will solve the same problem.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
我并不喜欢迄今为止提供的所有解决方案。
为什么?如果您要筛选的字段有超过 10 个值,则列表视图框也不太方便。我建议使用 django admin 的标准搜索字段功能,它将显示一个搜索字段:
I am not a fan of all solutions provided up to now.
Why? If, for a field that you want to filter by, you have more than 10 values, a listview box isn't that handy, too. I advice to use the standard search field capability of django admin which will show you a search field:
http://djangosuit.com/ 还提供列表过滤器的下拉菜单。
http://djangosuit.com/ also offers dropdowns for list filters.
最好的解决方案是在
admin/filter.html
中创建一个新模板并实现 @beholderrk 建议的 HTML 代码。刚刚为客户实现了它,效果很好。DropdownFilter 和RelatedDropdownFilter
的问题是它失去了正确的显示。它将显示True
、False
等,而不是Charfield(choices=xxx)
的翻译字符串。The best solution is to create a new template in
admin/filter.html
and implement the HTML code suggested by @beholderrk. Just implemented it for a client and it works great.Problem with
DropdownFilter and RelatedDropdownFilter
is that it loses the proper display. Instead of the translated strings forCharfield(choices=xxx)
, it will showTrue
,False
and so on.您能给出一个完整的例子吗?它显示像以前一样。
这是我的代码
这是屏幕截图
Could you please give a complete example. it shows like before.
here is my code
Here is the screenshot
几周前我也遇到了同样的问题。所以这个答案可能对未来的一些开发人员有用。
我设法通过编写自定义
template.html
解决了该问题
我现在已将代码捆绑在一个令人惊叹的包中,它对您也有同样的作用,这里是链接。
以下是如何实现可搜索下拉列表来代替默认列表:
1.安装:
此命令将在您的项目中安装最新版本的软件包。
现在,通过将
admin_searchable_dropdown
添加到settings.py
文件内的INSTALLED_APPS
中,将该包包含在您的项目中。2.用法:
假设您有以下型号:
并且您希望根据
company
过滤CarModelAdmin
中的结果。您需要在CarCompany
中定义search_fields
,然后定义如下过滤器:按照这些步骤操作后,您可能会看到过滤器为:
提供的功能:
auto_complete
功能),因此只要您使用的 Django 版本大于 2.0,就应该没问题。list_filters
,例如更改下拉列表上方的标题或自定义搜索逻辑等。I was struggling with the same problem some few weeks back. So this answer might be useful to some developers from the future.
I managed to solve the problem by writing a custom
template.html
I have bundled the code in an amazing package now that does the same for you, here's the link.
Here's how you can implement a Searchable Dropdown in place of the default List:
1. Installation:
This command will install the latest version of the package in your project.
Now, include the package in your project by adding
admin_searchable_dropdown
to yourINSTALLED_APPS
insidesettings.py
file.2. Usage:
Let's say you have following models:
And you would like to filter results in
CarModelAdmin
on the basis ofcompany
. You need to definesearch_fields
inCarCompany
and then define filter like this:After following these steps you may see the filter as:
Features Offered:
auto_complete
functionailty), so as long as the Django version you are using is greater than 2.0, you should be fine.list_filters
you may have, like change the Title above the dropdown, or a custom Search logic etc.str(obj)
要向 @beholderrk 和 @Gediminas 解决方案添加搜索功能,请使用以下命令
在
templates/admin
文件夹中创建searchable_dropdown_filter.html
并将以下代码粘贴到
admin.py 中
或filters.py
粘贴以下内容在您的管理实现中按如下方式使用它
这使用 select2 将搜索功能添加到下拉列表中。当您有大量要从中过滤的选项时,这非常方便。
To add search functionality to @beholderrk and @Gediminas solution use the follow
Create
searchable_dropdown_filter.html
intemplates/admin
folder and paste the following codeIn your
admin.py
orfilters.py
paste the followingIn your admin implementation use it as follows
This uses select2 to add search functionality to the dropdown. This is very handy when you have a ton of options you want to filter from.
谢谢@beholderrk、@gediminas 和@jk-laiho!我将其打包到可重用应用中。
安装:
在
settings.py
中启用:在
admin.py
中使用:如下所示:
Thanks @beholderrk, @gediminas and @jk-laiho! I packaged this into a reusable app.
Install:
Enable in
settings.py
:Use in
admin.py
:Here's what it looks like:
我无法评论答案,因此我将在这里添加到 beholderrk 的答案中。
dropdown_filter.html
的新模板,或类似的dropdown_filter.html
在
filters 中创建一个新的过滤器类.py
:现在您可以在管理类中使用此过滤器:
效果很好!
I cannot comment answers so I'll add to beholderrk's answer here.
dropdown_filter.html
or similardropdown_filter.html
create a new filter class in
filters.py
:now you can use this filter in your admin class:
Works great!
使用 feincms 中的 filter.html
Use filter.html from feincms
一个简单的选择是使用 django-grappelli,它将所有过滤器替换为下拉列表。
An easy option would be to use django-grappelli, which replaces all the filters with drop downs.
您可以将管理模板从 django 安装复制到项目中的 templates/admin 文件夹中。
然后,您需要在要在其中显示输出的表单或模板中执行以下两件事中的任意一项:
如果您正在使用表单,则您希望将列表选项发布回数据库,你会在你的 model.py 中,在你有选择的字段中,输入如下内容:
如果只是为了在页面上显示,那么你将在模板标签上输出这样的内容:
<前><代码><选择>;
{% for options in object.details.all %}
<选项> {{ object.choice }}
{% 结束 %}
You can copy the admin templates from the django installation into you templates/admin folder in your project.
Then you will need to do any of 2 things in the forms or templates you want to show your outputs in:
If you are working with a form, in that you would like the list choices to be posted back to a database, you would in your model.py, on the field you have your choices, put in some this like this:
If it is just to display on a page, then you will output on a template tag something like this: