如何在 Django 管理中对外键过滤器的 list_filter 标签进行排序?

发布于 2024-09-10 09:22:57 字数 1551 浏览 3 评论 0原文

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>

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

{% 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|dictsort:"display" %}
      <option {% if choice.selected %}selected{% endif %} value="{{ choice.query_string|iriencode }}">
        {{ choice.display }}
      </option>
    {% endfor %}
  </select>
</div>

因为我有很多标签,所以我使用了选择下拉菜单,但它也可以在标准“ul”列表上使用。现在,我终于将所有基于外键的过滤器按字母顺序排序(即使使用日期它也有效)。

如果您需要反向给药,可以使用 dictsortreversed 模板标签。

希望这对某人有帮助。

List_filter labels for foreign key filters in django admin are always ordered by id and that can cause a pretty mess when there are many filters in the list.

I have been searching for simple solution how to order those labels alphabetically or by date for some time now. It seemed that besides of using FilterSpec there is no solution for it.

Until I did this.

I have changed the template for filter.html (put it in admin folder in your templates directory) so it looks like this (found it somewhere on django snippets I guess):

{% 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>

And then I used 'dictsort:"name"' template tag on for loop so the template finally looked like this:

{% 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|dictsort:"display" %}
      <option {% if choice.selected %}selected{% endif %} value="{{ choice.query_string|iriencode }}">
        {{ choice.display }}
      </option>
    {% endfor %}
  </select>
</div>

I have used select drop-down since I had many labels, but it can be used on the standard 'ul' list too. Now I finally have all my foreign key based filters ordered alphabetically (and it works even if using dates).

If you need reversed dosting there is dictsortreversed template tag for that.

Hope that this helps someone.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

叹倦 2024-09-17 09:22:57

呃,问题本身就包含答案。很抱歉没有更好地构建它。

Errr, the question itself contains the answer. Sorry for not structuring it better.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文