小编tmi*_*van的帖子

如何在Django admin中为外键过滤器排序list_filter标签?

django admin中外键过滤器的List_filter标签总是按id排序,当列表中有许多过滤器时,这会导致相当混乱.

我一直在寻找简单的解决方案,如何按字母顺序或按日期订购这些标签一段时间.似乎除了使用FilterSpec之外,没有解决方案.

直到我这样做

我已经更改了filter.html的模板(将它放在模板目录中的admin文件夹中)所以它看起来像这样(在django片段的某处我发现它):

{% load i18n %}
<h3>{% blocktrans with title as filter_title %} By {{ filter_title }} {% endblocktrans %}</h3>
<div align="right">
  <select onChange="javascript:window.location = this.options[this.selectedIndex].value;" style="width: 80%">
    {% for choice in choices %}
      <option {% if choice.selected %}selected{% endif %} value="{{ choice.query_string|iriencode }}">
        {{ choice.display }}
      </option>
    {% endfor %}
  </select>
</div>
Run Code Online (Sandbox Code Playgroud)

然后我在for循环中使用'dictsort:"name"'模板标记,所以模板最终看起来像这样:

{% load i18n %}
<h3>{% blocktrans with title as filter_title %} By {{ filter_title }} {% endblocktrans %}</h3>
<div align="right">
  <select onChange="javascript:window.location = …
Run Code Online (Sandbox Code Playgroud)

django django-admin

7
推荐指数
1
解决办法
1878
查看次数

标签 统计

django ×1

django-admin ×1