Reputation: 1122
I generally use Python ORMs like Tortoise ORM or Ormar ORM (because they're async, integrate well with FastAPI, have very good performances (for Tortoise ORM), can use single Pydantic/type-hinted models (for Ormar). And of course also from habit.)
However, I have now a use case it seems I cannot do with Tortoise ORM neither Ormar ORM: I would like to do lazily apply custom SQL queries on existing querysets (by lazily, I mean the two consecutive queries need to be executed only once, later).
e.g. current simplified Tortoise ORM lazy query logic:
async def main() -> list:
await connect_db()
query_1 = Users.all().only("id", "name", "age")
query_2 = query_1.order_by("-age")
return await query_2
I would need to apply one query (say query_3
) but with custom SQL, so I would have something like that:
async def main() -> list:
await connect_db()
query_1 = Users.all().only("id", "name", "age")
query_2 = query_1.order_by("-age")
query_3 = query_2.custom_query("SELECT id, name, age FROM users WHERE age > 18;")
return await query_3
Can I do something like that with an ORM? Which one and how? Thanks!
Upvotes: 2
Views: 952