如何在Django自定义SimpleListFilter中使用Select2

如何在Django自定义SimpleListFilter中使用Select2,django,django-admin,jquery-select2,django-autocomplete-light,django-select2,Django,Django Admin,Jquery Select2,Django Autocomplete Light,Django Select2,我创建了一个自定义的SimpleListFilter类(下面的代码),并设法将选项显示为选择输入。我想添加的是Select2提供的功能,即能够在SelectInput中搜索选项 有人能告诉我如何做到这一点吗 自定义SimpleListFilter类: class AnswerTreeKnowledgeCodeListFilter(admin.SimpleListFilter): template = 'admin/dropdown_filter.html' title = _('

我创建了一个自定义的
SimpleListFilter
类(下面的代码),并设法将选项显示为选择输入。我想添加的是Select2提供的功能,即能够在SelectInput中搜索选项

有人能告诉我如何做到这一点吗

自定义SimpleListFilter类:

class AnswerTreeKnowledgeCodeListFilter(admin.SimpleListFilter):
    template = 'admin/dropdown_filter.html'
    title = _('Kenniscode')

    parameter_name = "knowledge_code"

    def lookups(self, request, model_admin):
        return [(instance.pk, str(instance)) for instance in AnswerTreeRepository.filter_by_level(0)]

    def queryset(self, request, queryset):
        # Custom queryset filtering here
dropdown_filter.html:(复制自我忘记的源代码,所以我不相信这段代码)

{%loadi18n%}
var go_from_select=function(opt){window.location=window.location.pathname+opt};
{{filter_title}}{%endblocktrans%}使用标题作为筛选器的{%blocktrans%}
    {%if选项|切片:“4:%”
  • {choices%%中的选项为%s} {{choice.display} {%endfor%}
  • {%else%} {choices%%中的选项为%s} {%endfor%} {%endif%}
{% 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 class="form-control" style="width: 95%;margin-left: 2%;"
        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>