tortoise / tortoise-orm

Familiar asyncio ORM for python, built with relations in mind
https://tortoise.github.io
Apache License 2.0
4.5k stars 368 forks source link

How to emulate such a request through Tortoise ORM? #418

Open markelovstyle opened 4 years ago

markelovstyle commented 4 years ago

Hello, how I can a similar SQL request be sampled via Tortoise ORM? Read the documentation, but it doesn’t give examples of working with date and time

SELECT uid, SUM(posts) AS post, SUM(fine) AS fines, date FROM statistic
WHERE date > DATE(date) <= NOW() AND date >= DATE_SUB(NOW(), INTERVAL 31 DAY)
GROUP BY uid ORDER BY post DESC
grigi commented 4 years ago

Oh, that's a good point. I don't think we have date manipulation filters right now? How did I miss that?

markelovstyle commented 4 years ago

Great! I’ll wait for the release.

Does Tortoise have the ability to query the database directly, as in Pony ORM?

grigi commented 4 years ago

Yes:

conn = Tortoise.get_connection('default')
val = await conn.execute_query_dict("SELECT * FROM event")
print(val)

Should really add that to the documentation...

grigi commented 4 years ago

I added an example in the docs: https://tortoise-orm.readthedocs.io/en/develop/examples/basic.html#manual-sql