CodeSamurai777
CodeSamurai777

Reputation: 3355

How to filter by date part in tortoise ORM

I just started using tortoise ORM. And was wondering is it all possible to filter the datetime field by its part. For example to filter out only rows which datetime column corresponds to a given month. Similarly as it is done in Django ORM that is .filter(datetime_field__month=6)

Upvotes: 5

Views: 3665

Answers (2)

When you use Tortoise ORM and specify that a field is a DatetimeField, you can access its parts, as for example the month, with datetime_field__month.

This only works with PostgreSQL and MySQL, not with SQLite.

You can find the different parts to which you can access here: https://tortoise-orm.readthedocs.io/en/latest/query.html#filtering

Upvotes: 1

andnik
andnik

Reputation: 2804

I have posted response in my question: How to use db functions in Tortoise ORM

For your problem I suppose your can use pypika.CustomFunction, here is a link: https://tortoise-orm.readthedocs.io/en/latest/functions.html#custom-functions

Unfortunately, this is only what I can help with, using pypika.functions.Extract didn't help me either. My assumption was to do EXTRACT (MONTH FROM "created_at") but Tortoise ORM doesn't allow me to put this field in filter query.

Upvotes: 3

Related Questions