Как добавить выпадающий выбор в форму поиска в админке django?
Можно ли настроить форму поиска в админке django, добавив пользовательский вход select, на основе которого впоследствии фильтровать набор запросов?
1 - Расширен файл search_form.html для заданной модели admin
{% load i18n static %}
{% if cl.search_fields %}
<div id="toolbar"><form id="changelist-search" method="get">
<div><!-- DIV needed for valid HTML -->
<label for="searchbar"><img src="{% static "admin/img/search.svg" %}" alt="Search"></label>
<input type="text" size="40" name="{{ search_var }}" value="{{ cl.query }}" id="searchbar" autofocus>
<input type="submit" value="{% trans 'Search' %}">
# start of custom code
<label for="search-type">Typ vyhledávání:</label>
<select name="search-type" id="search-type" form="changelist-search">
<option value="all">Vše</option>
<option value="pn">PN</option>
<option value="email">Email</option>
<option value="order-id">Id objednávky</option>
</select>
# end of custom code
{% if show_result_count %}
<span class="small quiet">{% blocktrans count counter=cl.result_count %}{{ counter }} result{% plural %}{{ counter }} results{% endblocktrans %} (<a href="?{% if cl.is_popup %}_popup=1{% endif %}">{% if cl.show_full_result_count %}{% blocktrans with full_result_count=cl.full_result_count %}{{ full_result_count }} total{% endblocktrans %}{% else %}{% trans "Show all" %}{% endif %}</a>)</span>
{% endif %}
{% for pair in cl.params.items %}
{% if pair.0 != search_var %}<input type="hidden" name="{{ pair.0 }}" value="{{ pair.1 }}">{% endif %}
{% endfor %}
</div>
</form>
</div>
{% endif %}
2 - Мне нужно получить выбранную опцию из html-тега select
при отправке. Это нужно для того, чтобы метод ModelAdmin get_search_results
мог быть настроен в соответствии с выбранной опцией.
def get_search_results(self, request, queryset, search_term):
...
# here I need to access the selected option aleng with the search term
# The search term typed into the input bar comes in the search_term variable
# based on the selected option I need to dynamically adjust the `search_fields`
...
return queryset, use_distinct
Похоже, что search_term строится в django\contrib\admin\views\autocomplete.py
в AutocompleteJsonView
, но я не уверен на 100%, так как не могу поймать его с помощью панели инструментов отладки.
Любое предложение, как использовать такой параметр select для динамической search_fields
конструкции, будет высоко оценено.