-
Notifications
You must be signed in to change notification settings - Fork 771
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Correct filtering of __in with array #935
Comments
What type of filter are you using? The CSV-based filter should translate this:
into this: MyModel.objects.filter(mysubmodel__in=[1,5,9]) |
I'm using ngResources (angularJS) on the client, it translates the javascript code:
into:
|
Two thoughts:
|
There is no adapter. There is an easy way to fix, namely:
but I thought it would be good if django-filter supported the AngularJS syntax of the box? Or is there is reason that
are treated differently? |
Depending on the filter, this:
should be treated like this:
How are you creating the |
It depends on the widget. Some widgets expect multiple values and get a list back from the QueryDict. Others expect just a single value. Something like a multiple values filter should do more or less the right thing already. |
Ok, I think I got this. First, a workaround for this bug is to do: So there seems to be a bug/unexpected behaviour on the FilterSet. My FilterSet is defined as:
However,
I have no idea how to fix this, and it has no hurry as there is an easy work-around, but it is definitely unexpected behaviour. I gues 'somewhere' there must be an if call, something like this:
|
How about this:
Does it not already do exactly what you want? The issue is that the |
Oh, wow. That sounds great! Does the new |
No — because that needs a CSV friendly filter... It's one way or the other (unless you declare two filters). The CSV stuff was introduced because API crafters tend to prefer it. |
Ok, thanks for the information. It seems to be a matter of taste then. I will go with the taste of the API crafters 👍 although it still feels a bit strange that it does not fallback in case of the __in expression, like explained before. I will close this issue, as you have answered all my questions. 🥇 |
Let's say I want to filter myModel that has a foreignkey to MySubModel, to all instances that have as MySubModel with the following ids: [1,5,9], so I send an API call to:
MyAPI/mymodel?mysubmodel__in=1&mysubmodel__in=5&mysubmodel__in=9
I would expect the correct result, namely:
MyModel.filter(mysubmodel__in=[1,5,9])
but instead it does:
MyModel.filter(mysubmodel__in=1).filter(mysubmodel__in=5).filter(mysubmodel__in=9)
(at least that is what I expect from the result).
Is it possible to fix this in django-filter? Or do I need a custom filter?
The text was updated successfully, but these errors were encountered: