MultipleChoiceFilter not working? #1603
-
I have the following model:
and the following filter:
however - "...?group_classification=A&group_classification=B" in my querystring generates no SQL criteria in the WHERE clause for the "group_classification" field. but if I change "group_classification = django_filters.MultipleChoiceFilter" to "group_classification = django_filters.ChoiceFilter", the SQL query will generate "WHERE group_classification IN (B)" Can anyone tell me what I'm doing wrong? |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 6 replies
-
Hi @sheckathorne — Not sure. I added a test in #1604 showing For FilterSet instance |
Beta Was this translation helpful? Give feedback.
-
Thank you for the quick reply! I found the issue and I'm not exactly sure the best way to handle it: For context, the choices for group_classifcation are:
What I found is that in django_filters.filterset.py:
The values from my querystring aren't coming through in the cleaned data because they are failing form validation. If selecting "A" and "B" from my choices, the value being validated in my form is "[A,B]" and not "A" and "B" separately. My model field group_classification is expecting a single value "A" or "B" and "[A,B]" is not an acceptable choice. The simple fix for my case was to prevent validation on that field since the form action is GET and not POST, but not sure if there's a better way. Hopefully this can help someone else in the future. |
Beta Was this translation helpful? Give feedback.
-
If you're interested: print(f.form.errors):
|
Beta Was this translation helpful? Give feedback.
-
For anyone else reading this, my solution was to extend the models.CharField class and overwrite the clean() and to_python() methods to allow for either string or list:
|
Beta Was this translation helpful? Give feedback.
-
Hi @sheckathorne — are you able to capture the traceback to the Something like this in your subclass should do it…
|
Beta Was this translation helpful? Give feedback.
Hi @sheckathorne — Not sure. I added a test in #1604 showing
MultipleChoiceFilter
working as expected.For FilterSet instance
f
, I suggest looking atf.form.is_valid()
,f.form.errors
,list(f.form["group_classification"].choices)
, andf.qs.query
. Hopefully something there will show up as not as you're expecting.