簡體   English   中英

Django管理員list_filter-下拉小部件

[英]Django admin list_filter - dropdown widget

我可以將相關對象的list_filter更改為下拉列表。 問題是當我想過濾相關對象的相關對象時:

class Match(Model):
    team = models.ForeignKey('Team'...)
    away_team = ... (doesn't matter)

class Team(Model):
    country = models.ForeignKey('Country'...)

當我想按Team過濾Match對象時,我做了:

list_filter = ['team__country']

dropdown此過濾器,我使用https://github.com/mrts/django-admin-list-filter-dropdown

 list_filter[('team',RelatedDropdownFilter)]

當我想按其Team Country篩選Match對象時:

list_filter = ['team__country']

但是,當我想從此過濾器創建下拉列表時,它不起作用:

list_filter = [('team__country',RelatedDropdownFilter)]

看起來與未指定RelatedDropdownFilter相同。

RelatedDropdownFilter

class RelatedDropdownFilter(RelatedFieldListFilter):
    template = 'django_admin_listfilter_dropdown/dropdown_filter.html'

模板

{% load i18n %}
<script type="text/javascript">var go_from_select = function(opt) { window.location = window.location.pathname + opt };</script>
<h3>{% blocktrans with title as filter_title %} By {{ filter_title }} {% endblocktrans %}</h3>
<ul class="admin-filter-{{ title|cut:' ' }}">
{% if choices|slice:"4:" %}
    <li>
    <select style="width: 95%;"
        onchange="go_from_select(this.options[this.selectedIndex].value)">
    {% for choice in choices %}
        <option{% if choice.selected %} selected="selected"{% endif %}
         value="{{ choice.query_string|iriencode }}">{{ choice.display }}</option>
    {% endfor %}
    </select>
    </li>
{% else %}

    {% for choice in choices %}
            <li{% if choice.selected %} class="selected"{% endif %}>
            <a href="{{ choice.query_string|iriencode }}">{{ choice.display }}</a></li>
    {% endfor %}

{% endif %}
</ul>

你知道我該怎么辦嗎?

您的模板正在檢查是否有四個以上的選擇:

{% if choices|slice:”4:” %}

我的猜測是,您正在查看的測試數據中最多有四個選擇。

只需刪除該檢查並始終在該塊中使用<select> ,或添加更多測試數據即可。 這些選擇是基於現有數據構建的,而不是簡單地包括所有可能的值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM