How do I do a multiple select in the Admin filters ?

2,058 views
Skip to first unread message

Hurlu

unread,
Jul 25, 2016, 11:25:29 AM7/25/16
to Django users
Hi everyone,

For the project I'm working on, I got a Dish Model in which, in order to filter different dishes for allergens or other reasons, I got a BooleanField to check if the dish is pork-free, another to check if it's nuts-free and a good other dozen.

As you would expect, displaying all thoses filters side by side would by very unesthetical and unpratical. So I aimed to reduce their number to two: One multiple select filter for "contains this:", and another for "does not contain:".

My researches so far were not very successful : the best I could find is this module , namely Django-adminfilters, that I could not quite get to work, as well as many not-so-helpful SO posts.

Could someone please enlighten me, be it with a way to do it or a workaround ?
Thanks a lot !

Constantine Covtushenko

unread,
Jul 25, 2016, 3:02:08 PM7/25/16
to Django users
Hi Hurlu,

I can suggest two approaches here.
  1. change your model so that it contains two fields: 'allowed' and 'disallowed'. Every of which is an array of allowed and disallowed items respectively. You can use JSON Field for that. This approach has benefit that your models looks simple and can be easily extended with additional allowed/disallowed items. In your example every time new valuable property identified your should extend model and handle migrations both schema and data. With this approach Django/admin can handle your requirements pretty straightforward.
  2.  create custom form with 2 custom fields: allowed and disallowed. Exclude all extra filters as an 'excluded'. And override 'save' method to map users data from use input to model filters. And do it before model is saved.
I hope that should solve your problem.
Regards

Hurlu

unread,
Jul 26, 2016, 3:39:30 AM7/26/16
to Django users
Hi Constantine,

Thanks for your advice! I'll try the 1st approach and report back when I get a result / completely give up this approach. (which may take quite a few days, as i'll have to reformat lots of things in lots of models :,) )

Thanks again for the advice !

Hurlu

unread,
Jul 27, 2016, 9:00:18 AM7/27/16
to Django users
Woo , finally did it !

For those coming after me , here is how I did :
                                                                    Your struggle probably comes from the self.value() never being a list , right ? Well then, if you override the template filter.html to add a "multiple" to the <select>, then , in your redefinition of the queryset() function, do a self.value = request.GET.getlist("[Insert parameter_name from your filter]"), then you'll get the list you clicked back in the Django admin !
 (The problem being that django uses request.GET.items() to retrieve the self.value(). Alas, as stated in this documentation, this method only gets the last item of the QueryDict object, unlike getlist() !

There is still one little problem to fix, being that , when loading the new page, the filter is being "resetted", containing only the last item of the QueryDict. I believe that's nothing that an handy JS /JQuery script can't handle though, so I may edit this comment adding the script when I manage to get it to work.

Cheers!
Reply all
Reply to author
Forward
0 new messages