Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django Admin Filter multiple select

In the django admin interface, more precisely in the Changelist, I'd like to have filters for excluding some foods of the list. However, having about 10 filters that all have the same options (Included, Excluded) on the same type of field seems a bit impratical.

So the idea was to implement a multiple select in the filters, so that in one box we can rule out multiple type of food. I tried overriding the filter.html and a dozen of plugins, but nothing worked and i'm out of ideas.

Here is what i want to obtain in the filters . Does anyone out there have an idea of whether it's possible or not, and if yes how ?

Thanks !

like image 810
Hurlu' Avatar asked Feb 07 '23 12:02

Hurlu'


1 Answers

Well, I managed to solve the problem by myself just today :

First step was to create a custom filter.html for this one filter, and add the multiple tag in the select. However, it would be too easy if it worked: it just kept filtering for the farthest element selected in the list .

So, instead of using self.value() in my queryset(), that never returned a list, I did a request.GET.getlist('[your_parameter_name]') to get the full list, as it always existed as a WSGIRequest, but that in the ChangeList view, django uses request.GET.items(), that fetchs only the last item.

Hope this'll help someone later!

like image 193
Hurlu' Avatar answered Feb 18 '23 05:02

Hurlu'