Add Startswith Operator
Closed this issue · 8 comments
It would be great to add support for the __startswith
QuerySet
feature. ~
is great, but it does an icontains
which completely bypasses a index on the DB. Starts with would allow us to maintain the index on a large tables for indexed fields.
hey @AngellusMortis,
thank you for the idea. Sounds interesting. I wonder if this concept should be extended to full SQL like
operator support, which will handle cases like "starts with" and "ends with" and more. What do you think about the possible syntax? The very first idea off the top of my head is to add like
operator for strings, and keep field ~ "value"
as a shorthand for field like "%value%"
. It's a bit verbose, but should be familiar for those who know SQL. However, maybe there could be better ideas.
thanks,
Denis
That sounds about right. `field like "value%" would make it verbose enough to handle this use case very well. It is certainly a lot better than anything I was thinking of.
Django has a lookup which supports starts with natively, We can simply add a new token and get it working right?
the discussion above is mostly not about the implementation, but about the query language design. Yes, we could probably add another operator for "startswith". And what's next? One more operator for "endswith"? And another one for "like"?
@stebunovd I was referring to the original issue that is "adding support for the starts with operator" I had the exact same issue and I solved it by adding a new token which seems like an easy fix if one only requires starts with operator and yes, a general operator would be better and would solve the problem in a much more efficient way.
What if we take operators from the regular expressions language? ^
for "starts with" and $
for "ends with". BTW, CSS specs uses them https://developer.mozilla.org/en-US/docs/Web/CSS/Attribute_selectors#syntax
one more idea: add full support for regular expressions by introducing a new regex
operator. Such a move will cover many use cases and AFAIK regular expressions are supported natively by all major database backends
Will this issue be merged ? I am planning to implement on my own fork.