tortoise-orm icon indicating copy to clipboard operation
tortoise-orm copied to clipboard

Support string queries

Open clayscode opened this issue 4 years ago • 2 comments

Is your feature request related to a problem? Please describe. It would be nice for Tortoise ORM to support string queries to make it easier to implement search functionality in web applications

Describe the solution you'd like Something like ModelName.fiter("name__icontains: 'Bill' age__gt: '32'")

Basically it would be the same as the Query functionality, but as a string.

clayscode avatar Nov 05 '21 12:11 clayscode

What about RawSQL?

long2ice avatar Nov 13 '21 13:11 long2ice

If you don't want to use RawSQL, maybe you could make yourself a helper function for it, something like this.

from ast import literal_eval
from typing import Any

def calculate(value: str) -> dict[str, Any]:
    sects = value.split(",")
    values = {}
    for sect in sects:
        key, raw = sect.split('=')
        values[key.strip()] = literal_eval(raw.strip())  # Safe eval
    return values

calculate("name__icontains='Bill', age__gt='32'")  # {'name__icontains': 'Bill', 'age__gt': '32'}`

These could then be passed in as kwargs to the filter method via ModelName.filter(**calculate("name__icontains='Bill', age__gt='32'"))

BobDotCom avatar May 18 '22 04:05 BobDotCom