-
Notifications
You must be signed in to change notification settings - Fork 770
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
filter on a field for a set of values (OR between values) #137
Comments
I have the exact same question. |
I have the exact same question.too |
I have the exact question too |
i have an idea: overwrite MultipleChoiceFilter: from django.forms.fields import MultipleChoiceField class MultipleFilter(MultipleChoiceFilter): class xxxFilter(django.filters.FilterSet): |
Update 2015-04-17: As @TankOs pointed out below a change in django-filter required this solution to be adjusted, I'm correcting it here in case anyone uses this code without reading further. @kmwenja also posted a nice solution below that includes sanitisation and type coercion (this solution assumes you're filtering on string fields). @apelliciari, I have the same use case as you (DRF, need comma separated OR filter), this is my solution: from django_filters import Filter
from django_filters.fields import Lookup
from .models import Product
class ListFilter(Filter):
def filter(self, qs, value):
value_list = value.split(u',')
return super(ListFilter, self).filter(qs, Lookup(value_list, 'in'))
class ProductFilterSet(django_filters.FilterSet):
category = ListFilter(name='categories__slug')
class Meta:
model = Product
fields = ['category'] To anybody else, YMMV outside use with Django REST Framework |
For the record, |
The |
+1. @ddevlin solution works perfectly, I suggest adding it to the lib. |
Thank you @ddevlin! |
Hmmm. This is curious. I'm looking at the source code for Anyone fancy chucking a PR together with a failing test? And if so, @ddevlin's fix in a separate commit. ;) |
Can anyone paste an example of how you're setting up the filter? Basically the
This sounds like the (There's a separate issue about the |
Also — can you confirm DRF and django-filter versions? Thanks! |
Hi @carltongibson, I don't believe this is a django-filter issue. When DRF instantiates the FilterSet, it passes DRF's |
But it should work like this, no...?
(This is more or less the example from the docs) So assuming (Thanks for coming back!) |
I spoke too soon, I wasn't aware of |
Is that an actual, "I put a breakpoint there, and that was the value"? — If so that's wrong. We should be getting the equivalent to my:
|
Actual, but it appears to be a side effect of how werkzeug's debugger represents dicts, when I use IPython I get the same |
Specify |
Just wanted to let you guys know that a change in django-filter requires an adjusted @ddevlin solution. ;-) from django_filters import Filter
from django_filters.fields import Lookup
class ListFilter( Filter ):
def filter( self, qs, value ):
return super( ListFilter, self ).filter( qs, Lookup( value.split( u"," ), "in") ) |
i think, most people cannot use |
Does @ddevlin solution work for any type of filter (ie Number, Char)? |
You might have to adjust types, but it should do (more or less) |
@carltongibson so does that mean inheriting from different filter types or actually typing the split values? |
Yeah, you'll have to implement the filter yourself (but it's only small). |
@carltongibson you were right, here's my implementation:
So for filtering integers:
|
Sorry to post this on this issue, but I really can't find the issue. @carltongibson . Is it closed? |
Hey @zoidbergwill — No problem — and "Not exactly". The are two options if you want to support the (For this latter suggestion see my similar comment on #187) If there's anything specific I'm happy for an issue on the (I really must sit down and give the docs the love they need.) |
I ended up using a custom simple It's not a difficult filter to write, but for some reason I expected it to work out the box. I guess it is more of a documentation issue. While reading these docs and seeing how lists worked in the queries when using the |
this is a recurring question and a requirement for many. I'm all in favor of having an implementation available in the framework. |
@tiagoboldt Are you talking about the Or filtering or the The Or filtering works — this ticket is only still open because I haven't had the time to sit down and go over every corner of it to make sure there's nothing lurking. As for supporting
PRs welcome. |
@carltongibson 1,2,3 syntax. I've been using django-rest for some time and every project the need for such custom filter appears. |
i just ran across this issue in a project i'm working on. i used some of the above code to come up with a similar solution that works across ManyToMany relationships. that is, i have a Foo model with bars=model.ManyToManyField(Bar). when i tried the code from @ddevlin and @kmwenja , i ran into the fact that Relationship fields do not support Lookup. however, you can use Q expressions to achieve the desired filter expression. Here's my replacement code:
In the FilterSet, i created a |
I'm going to close this as it's not a bug as it stands. We'll see if #259 makes sense for adding the |
I'm a little confused as to why this is closed. Maybe it's a documentation issue but MultipleChoiceFilter doesn't seem to behave as expected as Tuky is states at #137 (comment) I spent a good hour or so on this before I found this issue thread so at the very least the docs seem a nice big warning :-) |
What's the bug? Where's the failing test case demonstrating it? No one has been able to pin down the supposed issue. |
I am sorry for asking, but what is that works now? for filtering things such as ?a=1,2,3 |
From my understanding, it's: I totally agree with the need for a native filter as such, as it has been a need I've had in every project I've used django-rest. |
Yep. Check the docs, have a play. Any issues open a new ticket. |
Hi,
i don't know if is the right place to ask, because it's not an issue, just a question.
I'm using django-filter with Django-REST-Framework to build APIs for my app (great job by the way to both of you!)
I would like to know if there's a way to get this filter working
http://example.com/api/products?category=clothing,shoes
to get all products with category "clothing" OR category "shoes".
I've tried with
http://example.com/api/products?category=clothing&category=shoes
but as expected it does an AND between the filters and retrieve only the shoes products (i suppose because it's the last filter).
The text was updated successfully, but these errors were encountered: