-
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
Logical OR on multiple values for a CharField #1076
Comments
If an # declarative
class CharInFilter(BaseInFilter, CharFilter):
pass
class F(FilterSet):
name = CharInFilter(field_name='name', lookup_expr='in')
class Meta:
model = Device
fields = ['name']
# generative
class F(FilterSet):
class Meta:
model = Device
fields = {'name': ['in']} The call would look like:
|
Thanks for the suggestion! It's something I considered early on, but I'd prefer to stay away from CSV-formatted parameters. Partly because it complicates the logic needed to form a request, but also because it deviates from the way ChoiceFields are handled natively. For example, suppose the Device model was:
And we want to find devices named Foo, Bar, or Baz in SiteA or SiteB. If we used the BaseInFilter approach, the request string would look like
We end up mixing two different methods conveying the same logic. I could definitely see using the approach to distinguish between OR and AND logic though. |
So, it sounds like you want a In that case, you might be able to just define your own form field class that inherits class NonValidatingMultipleChoiceField(forms.MultipleChoiceField):
def validate(self, value):
pass
# Or inherit `filters.AllValuesMultipleFilter`
class NonValidatingMultipleChoiceFilter(filters.MultipleChoiceFilter):
field_class = NonValidatingMultipleChoiceField |
Aha, I think that works! I was focused on finding a way to override Is there any interest in extending one of the built-in filters to support this behavior? If so, I'd be happy to submit a PR. And if not I think the approach you've pointed out is perfectly fine. |
cc @carltongibson, but probably not. Part of the point of this package is to validate inputs. Removing validation is removing some of this value. Maybe it's something we could add as a docs snippet? |
Need ☕️ but, can't we use an In filter with the |
Out of the box, the only filters that support |
OK, good. ☕️ but 24hrs later. 🙂 But, what I'm thinking is that there's no reason why |
I mean, all re: Anyway, is there anything to do here? |
No. 😀 But thanks for the thoughtful input, as always! |
yeah. Also thanks @jeremystretch. Interesting issue. |
Use Case
Suppose I have a Device model defined like this:
And I have a Django REST API endpoint at
/devices/
which lists all Devices. What I'd like to do is turn a query such asinto the Queryset
so that any Device named Foo OR Bar OR Baz is returned. The response should not trigger a validation error if any of these names are not found.
When using a stock CharFilter on the
name
field, only the last parameter is passed to the filter, resulting in the QuerysetWhat I've Tried
MultipleChoiceFilter doesn't work because it requires a predefined set of
choices
to be provided.AllValuesMultipleFilter sort of works, but it populates a list of all available values from the database to check against. This is undesirable both because of the additional overhead introduced and because it raises a ValidationError if any of the provided values do not exist. I don't want to validate the provided values; I only want to match on them.
Providing a custom
method
on CharFilter doesn't seem to be an option because we need to provide a Django form field capable of accepting multiple values.Apologies if I've missed something obvious. I've looked around for other issues relating to this but didn't find anything quite the same (#137 was very similar).
The text was updated successfully, but these errors were encountered: