如何排序Django管理中的外键过滤器的list_filter标签? [英] How to sort list_filter labels for foreign key filters in Django admin?

查看:296
本文介绍了如何排序Django管理中的外键过滤器的list_filter标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有django管理员中的外键过滤器的List_filter标签总是按id排序,并且可能会导致列表中有许多过滤器的情况很糟糕。一直在寻找简单的解决方案如何按照字母顺序排列这些标签,或者按日期一段时间。似乎除了使用FilterSpec之外,还没有解决方案。



直到我这样做。



我已经更改了filter.html的模板(将其放在管理文件夹中在你的模板目录),所以它看起来像这样(发现它在django片段的某个地方我猜):

  {%load i18n% } 
< h3> {%blocktrans,标题为filter_title%}由{{filter_title}} {%endblocktrans%}< / h3>
< div align =right>
< select onChange =javascript:window.location = this.options [this.selectedIndex] .value; style =width:80%>
{%在选择中选择%}
< option {%if choice.selected%} selected {%endif%} value ={{choice.query_string | iriencode}}>
{{choice.display}}
< / option>
{%endfor%}
< / select>
< / div>

然后我在for循环上使用'dictionsort:'名称'模板标签,以便模板终于看起来像这样:

  {%load i18n%} 
< h3> {%blocktrans,标题为filter_title%}按{{filter_title}} {%endblocktrans%}< / h3>
< div align =right>
< select onChange =javascript:window.location = this.options [this.selectedIndex] .value; style =width:80%>
{%在选择中选择| dictsort:display%}
< option {%if choice.selected%} selected {%endif%} value ={{choice.query_string | iriencode} }>
{{choice.display}}
< / option>
{%endfor%}
< / select>
< / div>

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



如果您需要反向dosting,那么会有一个diralortreversed模板标签。



希望这有助于某人。

解决方案

本身包含答案。对不起,更好地构建它。


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.

解决方案

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

这篇关于如何排序Django管理中的外键过滤器的list_filter标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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