-
-
Couldn't load subscription status.
- Fork 442
Add __like and __ilike filters (#421) #954
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
base: develop
Are you sure you want to change the base?
Conversation
|
|
|
(FWIW, @abondar and @grigi seem to have thought the feature was worth adding.) |
|
I'd vote for this feature -- I could utilize it in my project. |
|
I figured out how to use and combined For anyone else that may stumble across this, here's what I did:
from tortoise.expressions import Q
import core
[...]
async def policy_list(filter_values: str, username: str):
user_object = await core.user.get({"username": username})
q_expression = Q()
for filter_value in filter_values.split(" "):
q_expression &= Q(name__icontains=filter_value)
if not user_object.full_admin:
sites = user_object.site_access.split(", ")
q_expression &= Q(site__in=sites)
policies_object = await core.policy.get(q_expression)
from tortoise.expressions import Q
import core
[...]
async def get(policy_filter: dict | Q | None = None):
if not policy_filter:
return await models.Policies.all()
elif isinstance(policy_filter, dict):
results = await models.Policy_Out.from_queryset(models.Policies.filter(**policy_filter))
elif isinstance(policy_filter, Q):
results = await models.Policy_Out.from_queryset(models.Policies.filter(policy_filter))
return results[0] if len(results) == 1 else results |
|
@simoncampion Please fix conflicts. |
|
@waketzheng Sorry for the late response. I'm currently traveling, but I'll be able to take a look at the end of this month. |
|
@waketzheng It looks like you've already fixed the conflicts. Let me know if there's anything else you need me to do. Aside: It seems to me that this commit you added to the PR (and possibly other commits---I haven't checked all of them) aren't necessary to resolve merge conflicts and unrelated to the functionality added by the PR. Am I missing something? Or did you not mean to add these commits to the PR? |
TODO List
mssql/oracle does not pass unittest yet, cloud you help to do that? |
|
@waketzheng I see that you forced Also see my note in the PR description:
|
|
I fixed the failing mssql tests. I spent some time trying to get oracle tests to run on my local machine in a dockerized setup, as I'd prefer not to install dependencies for those tests on my machine. Unfortunately, I haven't yet managed to get them to run. Is there documentation on how to run the oracle tests somewhere? I didn't find anything here. |
CodSpeed Performance ReportMerging #954 will not alter performanceComparing Summary
|
For me, most of the time, I don't care the SQL statement and db type. I just want |
Adds support for __like and __ilike filters (#421)
Description
Adds support for filtering by arbitrary LIKE patterns. For example:
await User.filter(name__like='J_hn%')I made three potentially contentious implementation decisions:
%and_with backslashes in the pattern string if they desire to match those characters literally. If they are not escaped, they are treated as SQL wildcards. The caller does not need to escape\to match it literally. As far as I can see, this is consistent with how__containsworks, for example. It also escapes\on behalf of the caller.__ilikeusing UPPER + LIKE. Therefore, it works with flavors of SQL that do not support the ILIKE operator, such as MySQL.__like, at least in the current implementation, is case-sensitive or case-insensitive depending on the flavor of SQL that is used. If desired, this might be changed, so that__likeis guaranteed to be case-sensitive independently of the underlying database. I did not try to enforce case-sensitivity because LIKE queries on the database and__likefilters in Tortoise would yield different results, which might be confusing.Motivation and Context
Fixes #421
(The issue is assigned to someone else but has not been worked on for over a year. I hope it's fine that I opened a PR addressing it.)
How Has This Been Tested?
I added tests in
test_filters.py. Someone with a good understanding of SQL injections should take a look at the tests and let me know if there are further tests I should add.Checklist:
The two tests
tests/test_default.py::TestDefault::test_defaultandtests/fields/test_time.py::TestDatetimeFields::test_updatefail when runningmake test_postgreslocally on my machine. However, that is the case on thedevelopbranch as well and seems unrelated to the changes I made.This is my first contribution to Tortoise ORM. Please let me know if there are more elegant ways to implement LIKE filters using existing functionality in the code base that I might not be familiar with.