Ограничить выпадающий список фильтра данных
Я пытаюсь ограничить выпадающий список в своем фильтре, сейчас он содержит все значения, которые имеются в БД.
Для этого я подставляю ему group, видимо это совсем не то, что нужно. По всей видимости все объекты попадают в выпадающий список из-за того, что нет ограничения в GroupFilter.
Views.py
group = Group.objects.filter(id_cs=request.user.profile.id_cs)
my_filter = GroupFilter(request.GET, queryset=group)
groups = my_filter.qs.order_by('-creation_date')
table = GroupsTable(groups)
Filters.py
class GroupFilter(filters.FilterSet):
class Meta:
model = Group
fields = ['id_filial', 'id_cs', 'id_gpu', 'search_name_group', 'start_date', 'end_date', ]
Как добавить это ограничение?
Мне удалось достичь свою цель следующим способом:
В файл html я добавил следующие строки
<form method="post" id="groupForm"
data-list-gpu-url="{% url 'main:ajax_load_gpu' %}"
novalidate>
...
</form>
<script>
let csId = '{{ id_current_cs|escapejs }}';
</script>
<script src="{% static 'main/js/load_gpu.js' %}"></script>
load_gpu.js
jQuery( function($) {
let url = $("#groupForm").attr("data-list-gpu-url"); // get the url of the `load_gpu` view
// let csId = document.getElementsById('id_current_cs'); // get the selected cs ID from the HTML input
$.ajax({ // initialize an AJAX request
url: url, // set the url of the request (= localhost:8000/main/ajax/load-gpu/)
data: {
'id_cs': csId // add the id_cs id to the GET parameters
},
success: function (data) { // `data` is the return of the `load_gpu` view function
$("#id_id_gpu").html(data); // replace the contents of the city input with the data that came from the server
}
});
});
gpu_dropdown_list_options.html
<option value="">---------</option>
{% for gpu in list_gpu %}
<option value="{{ gpu.id_gpu }}">{{ gpu.name_gpu }}</option>
{% endfor %}
В файл urls.py добавил:
path('ajax/load-gpu/', views.load_gpu, name='ajax_load_gpu'), # <-- this
# one here
Если вы знаете способ сделать это проще, прошу Вас подсказать решение.